0
votes

I have a firebase http cloud function

exports.createCharge = functions.https.onCall((data, context) =>{

Within this function, I am creating a Stripe charge

return stripe.charges.create(chargeInfo, { idempotency_key }, function (err, charge){

        if(err){
            return err.message;
        }else{
            return "done";
        }

    });

The stripe charge is the last line of code in the http function. My problem is the error message doesn't get returned back to the client, and the task.getResult() in my app comes back as null after completion. Also, variables do not persist outside of the stripe charge. err and charge must be used within the scope of the stripe charge function almost as if the stripe charge function is running in it's own environment. How can I return the err or charge back to the client?

1

1 Answers

2
votes

Stripe.charges.create is an asynchronous call and in order to return results/errors from an asynchronous call, Firebase function requires you return a Promise who will return the value [0]

So, in order to err or charge, instead of using callback like what code sample you've posted, promise.then should be used.

return stripe.charges.create(chargeInfo, { idempotency_key })
  .then((charge) => charge)
  .catch((error) => error);

Hope the above helps to address your issue.

[0] https://firebase.google.com/docs/functions/callable