0

Is there a way how to dynamically add multiple conditions to Firestore's get request?

I saw a few solutions there but they included multiple get() requests inside for/while loops. I would need to find a solution that adds multiple filters based on data in array(s).

Issue is that I have several filters with multiple choice values and I am not sure how many of them will user select.

citiesDb
 .where('city', '==', 'New York')
 .where('city', '==', 'Paris')
 .where('city', '==', 'London')
 // It can be multiple more based on let cities = [] (with size of 0 to many)
 // There can be also a few more arrays with filters
 .get()
Ondřej Ševčík
  • 1,049
  • 3
  • 15
  • 31

1 Answers1

1

Does it work for you?

let query = citiesDb;

for (const city of cities) {
  query = query.where("city", "==", city);
}

query.get().then(...) // result

  • Brilliant! I knew that javascript has some special way how to achieve that. I used your original solution but I bet that this for loop also works. Thanks very much. – Ondřej Ševčík Aug 18 '19 at 15:16