Firebase understanding snapshot.child()

19,627

Solution 1

is there a way to retrieve a parent node/object without pulling some of it's children?

The Firebase JavaScript API always retrieves the complete node. So: no, there isn't a way in the JavaScript API to get a shallow result/

why would one use this snapshot method child()?

If we compare snapshot.child("property") with snapshot.val().property. The DataSnapshot.child() method returns a DataSnapshot, from which you can get a ref again. The val() method deserializes the snapshot's value into JSON. So you'll have to construct your own ref if you'd need one. But the value of each depends highly on your use-case, so "why" is not something I can answer for you.

Solution 2

The snapshot is a exact picture of everything in the node at the time of the call. What it contains however, will vary depending on how you get the snapshot.

The example provided in the guide is slightly one-dimensional. In general you would not have a node called name with just one person listed.

A better example would be a node called users with data as such

users
  user_id_0
   firstName:
   lastName:
   age:
  user_id_1
   firstName:
   lastName:
   age:

When used with the Value parameter, the snapshot of the users node contains all of the node's children and all of the data within each child (in this case, all of the users and their data), and the block that handles it is called once. We use this to read, for example, all of the users in the users node and then iterate over the users for some specific data. We also use it to do multi-parameter queries, which are not supported directly by firebase. So for example, we want to get all users named Elmo, age 20.

The Add parameter reads each child, one at a time, calling the block once for child, which would be each user in this case. Typically we use this to keep UI tableView's updated (ObjC) so when a new child data is added to Firebase, all of the apps who are observing will be notified, so we can then update our UI table.

You cannot retrieve a parent object without also retrieving the children. However, you can directly access a child if you know the parent object if you are looking for a specific piece of data. So you could retrieve users/user_id_0/age

Share:
19,627
Michael Onubogu
Author by

Michael Onubogu

I'm just a guy who wants to learn stuff...badly...like really badly...like so badly that I wake up at 3:00am, sleep-walk to the restroom, and realize that it's actually my PC downstairs and I've pee....erm... lets just say I'm an obsessed with learning :D

Updated on June 04, 2022

Comments

  • Michael Onubogu
    Michael Onubogu almost 2 years

    consider this data structure referenced on the Firebase quick start guide (here)

    {"name": {"first": "Fred","last": "Flintstone"}
    

    The docs say that one can access the datasnapshot location of each child object of "name" returned from a query using:

    var ref = new Firebase("https://docs-examples.firebaseio.com/samplechat/users/fred");
    ref.once("value", function(snapshot) {
         var nameSnapshot = snapshot.child("name");
         var name = nameSnapshot.val();
    
         name === { first: "Fred", last: "Flintstone"}
    
         var firstNameSnapshot = snapshot.child("name/first");
         var firstName = firstNameSnapshot.val();
         firstName === "Fred"
    
         var lastNameSnapshot = snapshot.child("name").child("last");
         var lastName = lastNameSnapshot.val();
         lastName === "Flintstone"
    
         var ageSnapshot = snapshot.child("age");
         var age = ageSnapshot.val();
         age === null (because there is no "age" child in the data snapshot)
    });
    

    But what's a little weird about this is when the following lines are processed.

    var nameSnapshot = snapshot.child("name");
    var name = nameSnapshot.val();
    

    name.first, and name.last are also retrieved. So why would one use this snapshot method "child()"? Or rather when would it be beneficial to use this method, since when you pull the parent object, Firebase pulls all children, or is there a way to retrieve a parent node/object without pulling some of it's children? Then this method to me would make sense.

    Any information would be gratefully appreciated! Thanks