ltmccarthy
ltmccarthy

Reputation: 21

Why am I getting this firebase/firestore error?

Here is my query

 const query = messagesRef.where('members', 'in', [`${currentUserId}`]).orderBy('createdAt')

I have created an index as well.

This is the error I get

Error: {"code":"failed-precondition","name":"FirebaseError"}

Which I assume is usually an index thing

Upvotes: 0

Views: 436

Answers (1)

Wesley LeMahieu
Wesley LeMahieu

Reputation: 2613

Yes, the failed-precondition can be due to a missing index. It can also be manually thrown by a user though with any user-defined description, typically from within a Firebase Function. The category is the only thing that must confine to the convention:

https://cloud.google.com/datastore/docs/concepts/errors

Here's one example of such error common in App-Checked Firebase Functions:

if (context.app == undefined) {
  throw new functions.https.HttpsError(
    'failed-precondition',
    'The function must be called from an App Check verified app.')
}

You may want to check your Function Logs to get the actual error description. If the issue happens locally, then your firestore-debug.log file should contain some useful information. Just to be certain it's actually an index issue or not.

If the issue is happening only on production, you may want to verify the contents of your firestore.indexes.json and compare to what's inside the Firebase Console under https://console.firebase.google.com/u/0/project/<PROJECT-NAME>/firestore/indexes.

If it's actually an index issue, there will be a URL provided inside the logs that you can click which will generate the index for you inside Firestore:

https://firebase.google.com/docs/firestore/query-data/indexing?authuser=0&hl=en#create_a_missing_index_through_an_error_message

I hope this helps track down the issue! I used failed-precondition for several different error types, some unrelated to Firestore or indexes.

Upvotes: 1

Related Questions