Search code examples
node.jsangulargoogle-cloud-functionses6-promise

Function returned undefined, expected Promise or value- NodeJS


I have an angular app that is using firestore as the DB and Google cloud functions to handle the backend. When I run my app and click on pay to make the call to the Stripe API I get the following message on the log for the cloud functions.

Function returned undefined, expected Promise or value

I have been reading several stackoverflow questions and they talk about me returning whatever in the Promise .then() but I keep getting the same error. The good thing is that the actual value gets store in Firestore with no problem, so it seems to be more like a warning rather than an error since nothing breaks.

What am I missing?

exports.stripeCharges = functions.firestore
  .document("/payments/users/TAMO/{paymentId}")
  .onWrite((event, context) => {
    const payment = event.after.data();
    const paymentId = context.params.paymentId;
    if (!payment || payment.charge) return;

    return admin
      .firestore()
      .doc(`/payments/users/TAMO/${paymentId}`)
      .get()
      .then(snapshot => {
        return snapshot.data();
      })
      .then(customer => {
        const amount = payment.amount * 100;
        const idempotency_key = paymentId;
        const source = payment.token.id;
        const currency = "usd";
        const description = "Test Charge";

        const charges = {
          amount,
          currency,
          description,
          source
        };

        return stripe.charges.create(charges, { idempotency_key });
      })
      .then(charges => {
        return admin
          .firestore()
          .doc(`/payments/users/TAMO/${paymentId}`)
          .set(
            {
              charge: charges
            },
            {
              merge: true
            }
          );
      });
  });

Solution

  • I solved this warning by doing the following:

    if (!payment || payment.charge) return null;
    

    The line above checks if payment exists or if it has already been charged