Meteor.WrapAsync don't return value

前端 未结 1 982
半阙折子戏
半阙折子戏 2021-01-15 13:42

I been trying to make work Meteor.WrapAsync I have read Meteor wrapAsync syntax answer, this video https://www.eventedmind.com/feed/meteor-meteor-wrapasync and

相关标签:
1条回答
  • 2021-01-15 14:13

    You are wrapping the wrong function here, Meteor.wrapAsync transforms an async function (this means a function which transmits its result to the caller via a callback) in a synchronous one.

    The function you pass to Meteor.wrapAsync does not have a callback as final argument, you should instead wrap _stripe.charge.create.

    stripe.charge = function (stripeToken) {
      var _stripe = StripeAPI(stripeToken);
      var stripeChargeSync = Meteor.wrapAsync(_stripe.charge.create,_.stripe.charge);
      var response = stripeChargeSync({
        amount: 1000, // amount in cents, again
        currency: "cad",
        card: stripeToken.id,
        description: "paid@whatever"
      });
      return response;
    };
    

    If you want to handle errors, you should use a try/catch block when calling stripe.charge.

    try{
      stripe.charge(STRIPE_TOKEN);
    }
    catch(exception){
      console.log("Sorry we couldn't charge the money",exception);
    }
    

    I see you are logging your error using alert, are you trying to use Meteor.wrapAsync on the client ? Meteor.wrapAsync is meant to be used on the server because the environment needed to provide synchronous-looking execution is available in Node.js, not the browser.

    0 讨论(0)
提交回复
热议问题