【问题标题】:Angularfire2 - Query table with a paramAngularfire2 - 使用参数查询表
【发布时间】:2016-11-01 16:45:36
【问题描述】:

我正在尝试检索具有特定键的参数的所有项目。

这是用检索到的项目填充数组的函数:

    this.subscription = this.activatedRoute.params.subscribe(
     (param: any) => {
    let postId = param['id'];
    console.log(postId);
      this.postsService.subscribePostReplies(postId)
             .do(console.log)
            .subscribe(posts => { this.posts = posts; this.cdr.detectChanges(); });
     });

这是服务中的方法:

 subscribePostReplies(postKey: string) {
    return this.af.database.list('posts', {
        query: {
            orderByChild: 'repliedTo',
            equalTo: postKey
        }
    });
 }

问题是查询发生时页面刷新,我还收到以下警报: FIREBASE 警告:使用未指定的索引。考虑将 /posts 中的 ".indexOn": "repliedTo" 添加到您的安全规则中以获得更好的性能

【问题讨论】:

    标签: angular angularfire2


    【解决方案1】:

    你的警告很清楚。

    警告:使用未指定的索引。考虑将 /posts 中的 ".indexOn": "repliedTo" 添加到您的安全规则中以获得更好的性能

    ".indexOn": "repliedTo"@/posts 看起来像这样:

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

    请记住,这只是一个警告,开发不需要 indexOn。

    除非您使用 REST API,否则开发不需要索引。实时客户端库可以在不指定索引的情况下执行临时查询。性能会随着您查询的数据增长而下降,因此如果您预计要查询大量数据,请务必在启动应用之前添加索引。

    【讨论】:

    • 我也收到了这个警告。如您在此处所述,我的数据库规则正确指定了.indexOn。如果您删除 equalTo,警告就会消失。
    • 不知道为什么要删除equalTo,因为这是@Pete 试图实现的查询的全部要点。
    • 哦,我不想删除equalTo - 当然这是必不可少的。只是如果你这样做,警告就会消失。但是,我现在已经正确解决了我的问题。我没有将规则更改(本地 JSON 文件)发布到 Firebase。菜鸟失误。
    【解决方案2】:

    只需按照 firebase 文档中的说明添加 .indexOn 规则

    https://firebase.google.com/docs/database/security/indexing-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-14
      • 2017-01-31
      • 2018-07-21
      • 1970-01-01
      • 2018-07-07
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多