0

Here is my JSON and Code which gives only array but not it values. As I had used push it generating Unique Id from, which I am not getting. All the values that stored in that id.

{
  "nitin" : [ {
    "mobile" : {
      "-K7GalDi5aAahDENCRi" : {
        "name" : "file:///D:/Software%20Drive/addItem.html?category=mobile",
        "price" : 345,
        "quantity" : 34
      }
    },
    "perfume" : {
      "-K7K7HSu4rQNwbH3ud0H" : {
        "name" : file:///D:/Software%20Drive/addItem.html?category=perfume",
        "price" : 1000,
        "quantity" : 20
      }
    }
  }]
}

This is my code

  fbref.on("value", function(snapshot) {
      var newPost = snapshot.val();
      console.log(newPost);
      fbref.on("child_added", function(newPost,pre){
          newchild=newPost.val();
          console.log(newchild); 
          var n=newPost.key();
          console.log("this is key"+n);
          console.log("Name"+newchild.name);
          console.log("Price"+newchild.price);
          console.log("Quantity"+newchild.quantity);
      });  
  });
1

1 Answer 1

2

The child_added event will fire when a child node is added directly under the location that you're querying.

So if you're querying the root of the JSON you show, a child_added will fire for nitin. And then if we'd add puf, a child_added will fire for that too.

{
  "nitin" : [ {
    ...
  }],
  "puf": [ {
    ...
  }],
}

If you want to receive a child_added for items inside mobile or perfume, you will need to attach a child_added on the parent node of those:

fbref.child('nitin').child(0).child('mobile').on('child_added'...

Or:

fbref.child('nitin/0/mobile').on('child_added'...

See this jsbin for a working sample of this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Frank van Puffelen but this procedure is done by me. What I want is how to go for next object that is perfume. Again I have create a new variable as suggestion given by you, but it should check that automatically whether any more node are existing or not and display that node
How to get enter in perfume, mobile.... and get their keys under them and to display them. Also let me know how to create the indexes of array to increment so that I can avoid of that unique keys genereated by push and get the data using purely array based but not id.
"avoid of that unique keys generated by push and get the data using purely array based but not id" -> there's a reason Firebase recommends against using array indices: firebase.com/docs/web/guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.