Search code examples
javascriptfirebasegoogle-cloud-firestore

Getting [Error: [firestore/failed-precondition] The query requires an index. when added where and orderBy togather


I am getting the following error when I am adding the orderBy('crearedAt') on the Firestore collection.

[Error: [firestore/failed-precondition] The query requires an index.

my collection is

enter image description here

export const CommentsCollection = firestore().collection('comments');

When i am adding the orderBy('createdAt') like

 let commentsQuery = CommentsCollection.orderBy('createdAt').where('postId', '==', data.postId);

I have already created the index on the Firebase console.


Solution

  • When you call .orderBy('createdAt') the required index is ascending but in your screenshot, I see that it is set descending. So to solve this you have to create another index where you need to set createdAt ascending.