最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Giving Failed Precondition Error - Stack Overflow

programmeradmin0浏览0评论

Am getting the ff error with the ff code: Error:

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

My Code:

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

I can't seem to figure out why this is happening. I've tried searching on google but I dont seem to find a solution. What am I doing wrong ?

Thanks.

Am getting the ff error with the ff code: Error:

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

My Code:

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

I can't seem to figure out why this is happening. I've tried searching on google but I dont seem to find a solution. What am I doing wrong ?

Thanks.

Share asked Jan 27, 2020 at 15:19 marvin ralphmarvin ralph 1,2903 gold badges27 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

According to Firestore's documentation in the Manage indexes page

If you attempt a pound query with a range clause that doesn't map to an existing index, you receive an error.

You can use the Firebase Firestore web console to create the indexes manually for your query.

Otherwise, just run the query and it will throw an error with a URL to the Firestore console where you will get the option to automatically create this pound index.

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

The code block looks like the culprit because it has an equality clause bined with a range clause.

发布评论

评论列表(0)

  1. 暂无评论