Accessing data in firebase database

2019-07-27 23:56发布

问题:

So basically,I have a web application that uses firebase database. and I am having a hard time on how to access the encircled ones?

here is my code:

 function getDataFirebase() {
    return new Promise(function(resolve, reject){
        refReview.on("value", async function(snap){
            var data=snap.val();

            console.log("awdaw",data);

        })
    })
 }

回答1:

Loop over snapshot returned from firebase to get values inside the object.

function getDataFirebase() {
    return new Promise(function(resolve, reject){
        refReview.on("value", async function(snap){
            let rootkey = snap.key
            console.log(rootkey)
            snap.forEach(snapshot => {
                let childKey = snapshot.key
                console.log(childKey)
                Object.keys(snapshot.val()).map(k => {
                  console.log(snapshot.val()[k])
                })
            })
        })
    })
}

This case falls when we don't know the value of generated timestamps from firebase as child nodes.