Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firesotre query with null variable

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
}
// ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading