使用Key作为父级的Firebase索引

时间:2017-10-19 01:04:47

标签: javascript firebase firebase-realtime-database

我从Firebase收到错误:

  

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

我的数据结构是:

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(...

我在这里错过了一些关键的东西吗?有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

删除此:

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

并在“ checkins”节点内添加此行作为firebase建议,然后您将看到魔术:

".indexOn": "read"

因此您的规则应变为:

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