JavaScript Firebase: Query Snapshot Always Null

后端 未结 2 1631
無奈伤痛
無奈伤痛 2020-12-22 10:14

No matter what I do I can\'t seem to figure out a way to access the child \"onSite\", which shows as being there when I log snapshot.val(), but I cannot figure out how to ac

2条回答
  •  有刺的猬
    2020-12-22 10:48

    When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

    Your code needs to handle the list, by using Snapshot.forEach():

    firebase.database().ref().child("users").orderByChild('facebook_id').equalTo(fbID)
    .once("value").then(function(result) {
        result.forEach(function(snapshot) {
            console.log(snapshot.val());
            console.log(snapshot.child("onSite").val());
        });
    });
    

提交回复
热议问题