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

Firestore query – assign repetitive query elements to variable

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));

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 :

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));         
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