0

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:\Users\Suman\Kamakshi\Ganesh\Burrabazar\Cloudfunctions\functions\index.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.

1
  • 2
    You can only use await inside async functions. Commented Nov 22, 2021 at 17:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.