【问题标题】:Firebase Index with Key as Parent以键为父级的 Firebase 索引
【发布时间】:2018-03-30 23:48:36
【问题描述】:

我从 Firebase 收到一个错误:

错误:没有为读取定义索引。 FIREBASE 警告:使用未指定的索引。您的数据将在客户端下载和过滤。考虑将 /checkins 处的 ".indexOn": "read" 添加到您的安全规则中以获得更好的性能。

我的数据结构是:

checkins: {
  -KwlbakCMOcjs1UhlqLv: {
    read: true,
    user: "someuserid"
  },
  -KwlysmdbSCfTI_Nz0Wo: {
    read: false,
    user: "someotherid"
  }
}

如您所见,checkins 的直接子代是键。根据错误消息,我尝试了多种编写规则的方法,但错误仍然存​​在:

{
  "rules": {
    ".read": true,
    ".write": true,
    "checkins": {
      "$checkinid": {
        ".indexOn": "read"
      }
    }
  }
}

{
  ..
  "checkins": {
    "$checkinid": {
      ".indexOn": ["read"]
    }
  }
}

{
  ..
  "checkins": {
    ".indexOn": "read"
  }
}

{
  ..
  "checkins": {
    ".indexOn": ["read"]
  }
}

有趣的是:我试图将它切换到 indexOn 用户,但后来我也得到了一个错误(同样的想法 - 没有定义索引)。 checkins 不是任何事物的孩子,除了您现在看到的用于读/写的规则之外,没有其他规则。

如果有帮助,我的查询是:

db.ref( 'checkins' ).orderByChild('read').equalTo(false).on(...

我尝试使用user 作为索引:

db.ref( 'checkins' ).orderByChild('user').on(...

所以我在这里遗漏了一些关键的东西吗?有任何想法吗?谢谢!

【问题讨论】:

  • 最后两个.indexOn 定义应该可以工作,只要你的数据结构确实是/checkins

标签: javascript firebase firebase-realtime-database


【解决方案1】:

删除这个:

     "$checkinid": {
        ".indexOn": "read"
      }

并将这一行添加到“checkins”节点内作为 firebase 建议,然后你会看到神奇之处:

".indexOn": "read"

所以你的规则应该变成:

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

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2021-11-18
    • 1970-01-01
    • 2020-11-23
    • 2020-11-21
    • 2018-09-25
    • 2013-12-03
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多