我想获取像图像中那样的嵌套子对象,但它返回 null。我想要价格和数量的值并将它们显示在表格中。
检查此数据库镜像 here.
代码
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);
});
最佳答案
您指向了错误的路径,这就是您得到 null 的原因。 Firebase 文档如下:
If there is no data, the snapshot returned is null.
我无法从图像中看到您的整个数据库结构,但请尝试仔细检查路径,这是我的最佳猜测:
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)
})
})
关于javascript - firebase 嵌套对象返回 null - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49706730/