Home > other >  Firesotre query with null variable
Firesotre query with null variable

Time:10-20

I want to query a collection using multiple "where" like the example below

const queryRef = citiesRef
  .where('state',   '==', v1)
  .where('city',    '==', v2)
  .where('address', '==', v3);

The question is: how to check if v1, v2 or v3 ... is null so that i can remove it from the query?

CodePudding user response:

Since the where() method returns a Query, you can check for each value as follows:

let queryRef = citiesRef;
if (v1 !== null) {
  queryRef = queryRef.where('state',   '==', v1);
}
if (v2 !== null) {
  queryRef = queryRef.where('city',   '==', v2);
}
// ...
  • Related