Home > OS >  Firestore query - assign repetitive query elements to variable
Firestore query - assign repetitive query elements to variable

Time:12-23

I have many Firestore queries. They differ only with one element. Is it possible to assign repetitive elements to variable?

firstQuery = query(productEventsCollection,
                orderBy("created", "desc"),
                where("productEventType", "==", eventType),
                where("created", ">=", startDate),
                where("created", "<=", endDate),
                where("productSKU", "==", prodSku),
                limit(queryLimit));

to something like:

firstQuery = query(VARIABLE,
                limit(queryLimit));

CodePudding user response:

You can build an array of function calls that build up your query and then use the spread syntax ... to call the query() function with each item from the array as an argument:

const queryArgs = [productEventsCollection, orderBy("created", "desc"), where("productEventType", "==", eventType), where("created", ">=", startDate), where("created", "<=", endDate), where("productSKU", "==", prodSku)];
const firstQuery = query(...queryArgs, limit(queryLimit));         
  • Related