This logs the snapshot:
JavaScript
x
6
1
const db = firebase.firestore();
2
const collection = db.collection(`companies/${company}/meetings`);
3
let query = collection.where('start', '>=', new Date());
4
const snapshot = await query.limit(10).get();
5
console.log(snapshot);
6
This doesn’t:
JavaScript
1
9
1
const db = firebase.firestore();
2
const collection = db.collection(`companies/${company}/meetings`);
3
let query = collection.where('start', '>=', new Date());
4
if (branch) {
5
query = query.where('branch', '==', branch);
6
}
7
const snapshot = await query.limit(10).get();
8
console.log(snapshot);
9
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.