我试图从firebase数据库中获取子计数。而且每次执行代码时,我都会得到0而不是实际计数作为输出,或者是否有其他方法可以从数据库中获取子代计数。
this.itemsRef = firebase.database().ref('sample').child((new Date(), 'dd-mmm-yyyy').toLowercase()).child('dataContainer')
this.itemsRef.on('value', snapShot => {
this.state.count = snapShot.numChildren()
})
JSON数据:
"sample" : {
"20-jun-2018" : {
"dataContainer" : {
"1" : {
"header" : "sample",
"price" : 2.33
},
"-LFTL4hUnVr4WbZRMeBU" : {
"header" : "item",
"imageLink" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2Fshawaramas?alt=media&token=a7907c48-005d-4d52-bb6e-26e97bda0c4e",
"price" : "5.65"
}
}
},
"21-jun-2018" : {
"dataContainer" : [ null, {
"header" : "widget",
"imageLink" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2FMasala%20Dosai?alt=media&token=a103c1be-37a1-4ca3-9da8-5f87d5c870a8",
"price" : "8.68"
}, {
"header" : "images"
} ]
},
"22-jun-2018" : {
"dataContainer" : [ null, {
"header" : "sample data",
"imageLink" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2FDosai?alt=media&token=1191f1bb-6035-46b0-b24c-7b18bef65036",
"price" : "4.56"
}, {
"header" : "data"
} ]
}
}