0

I want to get the nested child objects like in the images but it is returning null. I want values of price and quantity and show them in table.

Check this database image here.

Code

var countRef = firebase.database().ref('Orders/' + listid);
countRef.on('value', snapshot => {
var b = snapshot.child("price").val();
var c = snapshot.child("quantity").val();
    console.log(c);
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ganesh
  • 5
  • 4

2 Answers2

1

You are pointing to the wrong path that's why you are getting null. Here is what the Firebase docs say:

If there is no data, the snapshot returned is null.

I can't see your entire database structure from the image but try to double check the path and here is my best guess:

var foodItems = firebase.database().ref(`Orders/'${listid}/foodItems`)
foodItems.on('value', snapshot => {
   // the snapshot should return the foodItems array
   snapshot.forEach((obj) => {
          console.log(obj.price)
          console.log(obj.quantity)
   })

})
Hamed Baatour
  • 6,664
  • 3
  • 35
  • 47
  • Can you please tel me how can i clear the table? I am using material dialog and material table. When button is clicked its shows dialog containing able, but every time button is clicked it adds new data below previous one. – Ganesh Apr 07 '18 at 12:56
  • @Ganesh post another question with the full code so I can help you. – Hamed Baatour Apr 07 '18 at 12:58
  • https://stackoverflow.com/questions/49707676/how-to-clear-table-inside-a-dialog-when-dialog-is-closed check it here – Ganesh Apr 07 '18 at 13:06
0

If your listid is and exist key, then you mistyped the path: fooditems so try this;

var countRef = firebase.database().ref('Orders/' + listid);
countRef.on('value', snapshot => {
if (snapshot.exists()) {
   var b = snapshot.child("fooditems/price").val();
   var c = snapshot.child("fooditems/quantity").val();
    console.log(c);
}
});
Cappittall
  • 3,300
  • 3
  • 15
  • 23