首页 > 解决方案 > Firebase 提供失败的前提条件错误

问题描述

使用 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);
        }
      );
    });
  }

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

谢谢。

标签: javascriptfirebasegoogle-cloud-platformgoogle-cloud-firestoreionic3

解决方案


根据管理索引页面中的 Firestore 文档

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

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

否则,只需运行查询,它就会向 Firestore 控制台抛出带有 URL 的错误,您将在该控制台中获得自动创建此复合索引的选项。

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

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


推荐阅读