ManPreet
ManPreet

Reputation: 77

Firebase Cloud Function Does Not Send Response

here is my code of http cloud function that reads some documents and then send response

res.set('Access-Control-Allow-Origin', '*');

    var orderId;
    var result = "";

    var userId;
    var promoCode;
    var promoRef;
    var userDocRef;

    var promoCodeDoc;

    //userId = req.body.userId;
    //orderId = req.body.orderId;
    promoCode = req.body.promoCode;

    //userDocRef = db.collection("Users").doc()
    promoRef = db.collection("PromoCodes").doc(promoCode);


    var transaction = db.runTransaction(t => {
      return t.get(promoRef)
        .then(promoCodeDoc => {
            if(promoCodeDoc.exists){
              result = "OK";
              res.json(result);
            }else{
              result = "Invalid Promocode!";
              res.json(result);

            }
          //t.update(cityRef, {population: newPopulation});
          return true;
        });
    }).then(result => {
      console.log('Transaction success!');
      return true;
    }).catch(err => {
      console.log('Transaction failure:', err);
    });

    return Promise.all(transaction());

But This is not sending the response because functions ends but Firestore Transaction is still runnning in background . Any Solution to my problem ?

Upvotes: 1

Views: 797

Answers (1)

John
John

Reputation: 10009

Promise.all() expects a single array of promises as its argument, but you're not giving it an array argument. Secondly, the transaction variable is a promise, not a function. You can't call () a promise.

So I think the correct code would be return Promise.all([transaction]). This being said, you only have one promise so you don't need Promise.all and can just return transaction.

Not sure if this will solve all your problems though. If you log into the firebase console, navigate to the functions section, there's a "Logs" tab that allows you to see debugging output from your function executions. It might help you track down all the problems. I imagine there are already console errors logged pointing out the fact that transaction() is not a function.

Upvotes: 1

Related Questions