javascript - Getting nested data from firebase database by "onchildchanged" and "onchildadded" returns something unrecognizable -
screenshot of console displays output i.e. child added in messages directory of 1 of users.
, must noticed have displayed short portion of console, console displaying similar output each , every messages. if more screenshots required clarification, please tell me, add them!
json code:
{ "tf42gluiuvbf1ygmx1cseyd6evc3" : { "intents" : { "intentfields" : "", "intentname" : "" }, "messages" : { "-kvivgc7zg051emxp0-5" : { "name" : "ed", "photourl" : "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-512.png", "text" : "hello, i'm ed, personal assistant cum friend", "timestamp" : "1476880306" }, "-kw3c8azybp_-bilwmnv" : { "name" : "aakash bansal", "photourl" : "https://lh5.googleusercontent.com/-oqya4hxvycc/aaaaaaaaaai/aaaaaaaahko/ov0a0p0ljiy/s96-c/photo.jpg", "text" : "hi", //this message displayed in console above. "timestamp" : "1478613047" } } }, "gfiprphchemkqg2tgui1aolir3x1" : { "intents" : { "intentfields" : "", "intentname" : "" }, "messages" : { "-kvivgc7zg051emxp0-5" : { "name" : "ed", "photourl" : "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-512.png", "text" : "hello, i'm ed, personal assistant cum friend", "timestamp" : "1476880306" } } }
}
this node.js code:
ref.on("child_changed", function(snapshot){ snapshot.ref.child("messages").on("child_added", function(itemsnapshot) { var mytemp = itemsnapshot.child("text"); console.log(mytemp); }); });
note viewed similar questions on stackoverflow when implemented them, came know belonged outdated version of firebase , didn't work in current version.
edit using following code, i'm getting null
each child added. also, want ask how parent's or user's id child has changed. code is:
ref.on("child_changed", function(snapshot){ //using limittolast(1) last value snapshot.ref.child("messages").orderbykey().limittolast(1).once("value", function(itemsnapshot) { var mytemp = itemsnapshot.child("text").val(); console.log(mytemp);
}); });
screenshot of console after using code got answer aove question, remaining doubt how parent of node child has changed?
please feel free ask more clarification if , when needed
thanks!!
i think may looking this:
ref.on("child_changed", function(snapshot){ snapshot.child("messages").foreach(function(itemsnapshot) { var mytemp = itemsnapshot.child("text").val(); console.log(mytemp); }); });
Comments
Post a Comment