Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  119] [ 2]  / answers: 1 / hits: 21030  / 9 Years ago, tue, june 30, 2015, 12:00:00

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


More From » firebase

 Answers
18

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.


[#65979] Sunday, June 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;