正确的查询结构.无法获取日志输出

proper structure for query. not getting log output

本文关键字:获取 日志 输出 结构 查询      更新时间:2023-09-26

我正试图在firebase中对我上传的一些测试数据执行一个简单的equalTo()。这就是代码。

var ref = new Firebase("https://crackling-fire-1105.firebaseio.com/business");
ref.orderByChild("city").equalTo("Pétion-Ville").on("child_added", function(snapshot) {
  console.log(snapshot.key());
});

控制台里什么也没有。

如果我在自己的代码旁边使用firebasedoc代码,它就可以工作了。https://www.firebase.com/docs/web/guide/retrieving-data.html#section-查询

    var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
  console.log(snapshot.key());
});

索引出错。我正在获取。(已在下面的评论中解决)

FIREBASE警告:使用未指定的索引。考虑在/business的安全规则中添加".indexOn":"city"以获得更好的性能

第一次拿到后,我加了.indexOn:"城市"。我仍然收到那个错误,我在控制台上什么也没有得到。

这些是安全和规则控制台中的规则。

{
    "rules": {
        ".read": true,
        ".write": true,
        ".indexOn": "city"
    }
}

json结构如下:

{
 business {
     Joe's Designs: { 
                 "accountid"
                 "address"
                 "city"
                 "email"
                 "heading"
                 "headingid"
                 "objectId"
                 "phonenumber1"
                 "website"
                 }
            }
}

如有任何帮助,我们将不胜感激。非常感谢。

在需要索引的键下面指定.indexOn

{
   "rules": {
     "business": {
        ".indexOn": "'"city'""
     }
   }
}

这使您可以为任何顶级密钥设置.indexOn规则。

从你的数据来看,你似乎在逃避密钥。我不确定您为什么要这样做,但这将要求您使用转义值进行索引和查询。

JSBin演示

var ref = new Firebase("https://crackling-fire-1105.firebaseio.com/business");
ref.orderByChild("'"city'"").equalTo("Pétion-Ville").on("child_added", function(snapshot) {
  console.log(snapshot.val());
});