JavaScript Firebase:查询快照始终为空 [英] JavaScript Firebase: Query Snapshot Always Null

查看:69
本文介绍了JavaScript Firebase:查询快照始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我做什么,我似乎无法找到一种方法来访问子节点onSite,当我记录snapshot.val()时,它显示在那里,但我无法弄清楚如何访问它。

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 access it.

代码:

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

以下是回复:

Here is the response:

它不应该为空,它应该是假的。我不能做孩子(4mUUjF ...)。child(onSite)。val()因为我不知道在查询之前ID是什么。使用每个循环不起作用,它只循环通过第一级,即ID。

It shouldn't be null, it should be false. I can't do child("4mUUjF...").child("onSite").val() because I don't know what the ID is before the query. Using an each loop doesn't work, it only loops through the first level, which is the ID.

推荐答案

执行时针对Firebase数据库的查询,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果列表。

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.

您的代码需要使用来处理列表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());
    });
});

这篇关于JavaScript Firebase:查询快照始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆