Recently I’ve started working on a project based on firebase cloud functions and firestore database. I’m writing a cloud function trigger function which will query a “Collection group”, on a new document being created.
Below is the cloud function code file:
exports.findDealsOnBuy = functions.firestore .document('businessmen/{businessmenId}/buy/{buyId}') .onCreate((snapshot, context) => { const businessmenId = context.params.businessmenId; const buyId = context.params.buyId; const buy = snapshot.data(); functions.logger.info('businessmenId : ', businessmenId, ' buyId : ', buyId, ' buy : ', buy ); const sellGrpRef = admin.firestore().collectionGroup('sell'); const querySnapshot = await sellGrpRef.whereEqualTo('goodsName', '==', buy.getGoodsName()) .whereEqualTo('goodsLocation', '==', buy.getGoodsLocation()) .whereEqualTo('status', '==', 1) .whereEqualTo('status', '==', 5) .whereLessThanOrEqualTo('bestPrice', '<=', buy.getBestPrice()) .orderBy('bestPrice', 'desc') .get(); if (querySnapshot.empty) { console.log('No matching documents.'); return; } querySnapshot.forEach((doc) => { console.log(doc.id, ' => ', doc.data()); }); });
But while compiling i am being thrown the below error
> C:UsersSumanKamakshiGaneshBurrabazarCloudfunctionsfunctionsindex.js > 31:31 error Parsing error: Unexpected token sellGrpRef
I tried a lot but I am unable to find a clue how to resolve this. Requesting help to resolve.
Advertisement
Answer
I am sharing now that I found the await documentation in MDN Web Doc.
To wait for a Promise, use the await operator. Within standard JavaScript code, it can only be used inside an async function.
You can use await within a function if you use the async keyword before the function definition. When you wait for a promise to settle, the function is stopped in a non-blocking manner. You get the value back if the promise is kept. The rejected value is thrown if the promise fails.