Skip to content
Advertisement

Conditional where clause in firestore queries

I have fetch some data from firestore but in my query I want to add a conditional where clause. I am using async-await for api and not sure how to add a consitional where clause.

Here is my function

JavaScript

In my main function I am getting a param called ‘type’. Based on the value of that param I want to add another qhere clause to the above query. For example, if type = 'nocomments', then I want to add a where clause .where('commentCount', '==', 0), otherwise if type = 'nocategories', then the where clause will be querying another property like .where('tags', '==', 'none')

I am unable to understand how to add this conditional where clause.

NOTE: in firestore you add multiple conditions by just appending your where clauses like – .where("state", "==", "CA").where("population", ">", 1000000) and so on.

Advertisement

Answer

Add the where clause to the query only when needed:

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