Skip to content
Advertisement

Firestore: Conditional query not executed

This logs the snapshot:

const db = firebase.firestore();
const collection = db.collection(`companies/${company}/meetings`);
let query = collection.where('start', '>=', new Date());
const snapshot = await query.limit(10).get();
console.log(snapshot);

This doesn’t:

const db = firebase.firestore();
const collection = db.collection(`companies/${company}/meetings`);
let query = collection.where('start', '>=', new Date());
if (branch) {
  query = query.where('branch', '==', branch);
}
const snapshot = await query.limit(10).get();
console.log(snapshot);

Does anyone know why?

Advertisement

Answer

Since you are combining the '>=' and '==' operators, Firestore needs to build an index for this query.

If you catch the error with a try/catch block, you will see the corresponding error, and, even better, the error message includes a direct link to create the missing index in the Firebase console.

See the doc here for more details on indexing in Firestore.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement