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);
});
0

2 Answers 2

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)
   })

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

3 Comments

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 post another question with the full code so I can help you.
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);
}
});

Comments

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.