【问题标题】:Firebase query is not returning any results from the databaseFirebase 查询未从数据库返回任何结果
【发布时间】:2014-12-14 15:54:09
【问题描述】:

我正在尝试在 Firebase 中进行简单搜索,以从我的 Firebase 数据中返回数据,但没有任何返回。

JSON 结构

{
    "-JZQzhqTI6NUQMfgHwQc" : {
        "Divisions" : {
            "-JZLh2UYKi5IvphLtX4I" : true,
            "-JZLh2Uc5HhffF1ki4H1" : true
        },
        "SportTypes" : {
            "-JZLOh7KtM78I5wAL6Ua" : true
        },
        "name" : "winnipegjets",
        "nickname" : "Jets",
        "teamname" : "Winnipeg Jets"
    }
}

我进行搜索的 Firebase 代码如下

   new Firebase("https://sizzling-fire-6197.firebaseio.com/teams")
            .startAt('Winnipeg Jets')
            .endAt('Winnipeg Jets')
            .once('value', function(snap) {
                console.log('matching team is', snap.val())
            });

但是一旦代码被执行,它就会返回 null。还有一种方法可以搜索特定字段。

【问题讨论】:

标签: firebase


【解决方案1】:

看起来您有一组团队,第一个位于名为 -JZQzhqTI6NUQMfgHwQc 的节点中(可能是通过调用 push 生成的)。它有一个属性name,其值为winnipegjets,还有一个属性teamname,其值为Winnipeg Jets

Firebase query you can filter nodes in two ways:

  • 他们的名字
  • 按他们的优先级

您的节点被命名为 -JZQzhqTI6NUQMfgHwQc 并且它具有优先级(在您发布的 JSON 中未显示)。这些是您可以过滤的仅有的两个值。

如果您还没有使用优先级,您可以考虑将nameteamname 设置为优先级(查看setPriority 的文档)并对其进行过滤。

看起来您已经在尝试使 name 属性独一无二。在这种情况下,您可以考虑按如下方式创建结构:

{
    "winnipegjets" : {
        "Divisions" : {
            "-JZLh2UYKi5IvphLtX4I" : true,
            "-JZLh2Uc5HhffF1ki4H1" : true
        },
        "SportTypes" : {
            "-JZLOh7KtM78I5wAL6Ua" : true
        },
        "nickname" : "Jets",
        "teamname" : "Winnipeg Jets"
    }
}

代码类似于:

var ref = new Firebase('https://sizzling-fire-6197.firebaseio.com/teams');
ref.child('winnipegjets').set(...);

【讨论】:

    【解决方案2】:

    如果您没有设置优先级。然后您需要 orderByChild,以便 firebase 返回结果。在你的情况下:

    new Firebase("https://sizzling-fire-6197.firebaseio.com/teams")
                .orderByChild('teamname')
                .startAt('Winnipeg Jets')
                .endAt('Winnipeg Jets')
                .once('value', function(snap) {
                    console.log('matching team is', snap.val())
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2011-04-17
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多