【问题标题】:Firebase Giving Failed Precondition ErrorFirebase 提供失败的前提条件错误
【发布时间】:2020-01-27 15:19:09
【问题描述】:

使用 ff 代码得到 ff 错误: 错误:

{"code":"failed-precondition","name":"FirebaseError","__zone_symbol__currentTask":{"type":"macroTask","state":"notScheduled","source":"setTimeout","zone":"angular","cancelFn":null,"runCount":0}}

我的代码:

getOldPosts(forum_id: string, user_id: string, start_key?: string): Promise<Posts[]> {
    return new Promise((resolve, reject) => {
      let olderPosts: Posts[] = [];
      let _5daysAgo = this.dateTime.getFiveDaysAgoDate();

      const postsRef = this.afirestore.collection<Posts>('posts').ref;
      const query = start_key == undefined || start_key == null
        ? postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id)
        : postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id).startAt(start_key).limit(50);

      query.get().then(
        res => {
          res.docs.forEach(doc => {
            olderPosts.push(doc.data() as ForumPosts);
          });
          resolve(olderPosts.reverse());
        },
        err => {
          reject(err);
        }
      );
    });
  }

我似乎无法弄清楚为什么会发生这种情况。我试过在谷歌上搜索,但我似乎没有找到解决方案。我做错了什么?

谢谢。

【问题讨论】:

    标签: javascript firebase google-cloud-platform google-cloud-firestore ionic3


    【解决方案1】:

    根据 Firestore 在 Manage indexes 页面中的文档

    如果您尝试使用不映射的范围子句进行复合查询 到现有索引,您会收到错误。

    您可以使用 Firebase Firestore Web 控制台为您的查询手动创建索引。

    否则,只需运行查询,它就会向 Firestore 控制台抛出错误,其中包含一个 URL,您可以在该控制台中选择自动创建此复合索引。

    postsRef.where('createdDate', '<=', _5daysAgo).where('forumId', '==', forum_id).where('memberId', '==', user_id)
    

    代码块看起来像罪魁祸首,因为它有一个相等子句和一个范围子句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-29
      • 2018-08-12
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2017-01-08
      • 2016-04-05
      • 2014-03-31
      相关资源
      最近更新 更多