Meteor Method Returns Undefined in React Native

Hi, I’m not quite understanding my use case with async and await. I’m using react native that is connected to a meteor backend with methods.

Everytime I click charge payment (through stripe) it waits until the process is done but then only returns undefined. Any help will be very appreciated

React native client:

        if (formFieldsValid) {
            return new Promise((success, failure) => {
                Meteor.call('checkoutCall', value, stripeToken, (error, result) => {
                    if (error) {
                        console.warn('Error: ' + error);
                        failure(error);
                    }
                    else {
                        console.warn('Success: ' + result);
                        success(result);
                    }
                });
            });
        }

Meteor server backend

Meteor.methods({ 
  async checkoutCall(value, stripeTokenInfo) {
    const stripeToken = stripeTokenInfo;
    const userValue = value;

    const charge = await stripe.charges.create({
      amount: 1000,
      currency: 'usd',
      source: stripeToken
    }, function(err, charge) {
      console.log(err, charge);
      if (charge) {
        return charge;
      } else {
        return err;
      }
    });
  },
});

You’re using a callback on your await stripe.charges.create and then trying to return a value from within that. Try something like this (untested):

Meteor.methods({
  async checkoutCall(value, stripeTokenInfo) {
    const stripeToken = stripeTokenInfo;
    const userValue = value;

    try {
      const charge = await stripe.charges.create({
        amount: 1000,
        currency: 'usd',
        source: stripeToken
      });
      return charge;
    } catch (err) {
      throw new Meteor.Error('ER123', err.message);
    }
  },
});

BTW, userValue is never used in your method.

1 Like

Working!

Thanks Rob, I would have been stuck trying the same variations of my old solution

1 Like