0

Firebase db Sample data arrangment

How do I access the "name" tag and its values under each unique firebase keys. Please refer to the attached screenshot of my sample datastructure.

Note: Here, the keys under "Tags" are generated through push function.

I have tried few things in JS but not working.

ref.child("users").child("Tags").limitToFirst(1).on("child_added", function(snapshot) {
console.log(snapshot.val()); });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user1322692
  • 173
  • 1
  • 11

2 Answers2

0

try this

ref.child("users").child("Tags").on("child_added",function(snapshot) {
    snapshot.forEach(function(snap){
        console.log(snap.val())
    });
}
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • tried but No output, no error in console log. child("Tags") is not showing any results. If I remove it then able to log some info. Why is that so? Any idea... – user1322692 May 31 '17 at 12:19
0

You can access the child property of the snapshot with its child() method:

ref.child("users").child(pushidofparent).child("Tags").on("child_added",function(snapshot) {
    console.log(snapshot.child("name").val())
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If I use "child("Tags") " in query its not showing any results or error in console log. If i remove it, snapshot value is showing but if added then blank. No error too. its not recognizing Tags are its child but why? – user1322692 May 31 '17 at 17:41
  • Yeah, I now see you have an extra level in your JSON. Do you know that top-level push ID? – Frank van Puffelen May 31 '17 at 21:28
  • if you are asking before "users" then its root of database. "users" do have some siblings but no parent node, only root is the parent to it. just seen your code, are you referring to "pushidofparent = key id of Tags"? – user1322692 Jun 02 '17 at 02:39
  • If I give the keyid of Tags node, I am able to access the "name" key value pair. I have a question here, I have never used Keyid in my other queries for similar node and I could perfectly access Tags like node. But why only for this need key id? any issue with my structure? appreciate your pointers on this. – user1322692 Jun 02 '17 at 02:50
  • It looks like you're trying to categorize the items. Your current structure allows you to efficiently read the categories for an item. But it does not yet allow efficient querying of the items for a category. To allow that, you should augment your data structure. See my answer here for an example: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 02 '17 at 07:53
  • gone through it. Thank you for the detailed explanation. I shall flatten the data for better access. – user1322692 Jun 02 '17 at 19:15