Meteor:异步回调问题

Meteor: Async Callback issues

本文关键字:问题 回调 异步 Meteor      更新时间:2023-09-26

当前我让用户点击提交,然后发生一个点击事件,其中创建了一个令牌并调用了一个方法。我想做的是在充电后得到一个回调,告诉你充电是否成功。如果成功,它将运行router。转到确认页面。如果不成功,它会让用户知道该卡已被拒绝。以上所有内容我都能编写出来,只是尽管不断地修改,我似乎不知道如何将消息传递回事件。

这是我的服务器端方法:

  Meteor.methods({
    'chargeCard': function(token,amount,email) {
      var Stripe = StripeAPI('where the key info guys');
      // get a sync version of our API async func
      var stripeCustomersCreateSync=Meteor.wrapAsync(Stripe.customers.create,Stripe.customers);
      // call the sync version of our API func with the parameters from the method call
      var result=stripeCustomersCreateSync({
        description: 'Woot! A new customer!',
        card: token,
        email: email
      }, function(error,result) {
        if(error) {
          return error;
        }
        return 'Success';
      });
      return result;
    }
});

和我的客户端方法:

    Stripe.card.createToken({
      number: $('#cc-number').val(),
      cvc: $('#card-cvc').val(),
      exp_month: expM,
      exp_year: expY,
      name: $('#fn').val(),
      address_zip: $('#postCode').val()
    }, stripeResponseHandler);
  }
  function stripeResponseHandler(status, response) {
    var $form = $('form');
    if (response.error) {
      // Show the errors on the form
      $form.find('.validation').text(response.error.message);
      return false;
    } else {
      var token = response.id;
      var amount = 15000;
      var Payid = $('#pid').text();
      var userEmail = Leaguemembers.findOne({_id: Payid}).Email;
      Meteor.call('chargeCard', token, amount,userEmail, function (error, result) {
        console.log(error,result); alert(result); alert(error);
        }
      );
    }
  };

如有任何帮助,我们将不胜感激。

编辑:

我回到后端,可以看到通过console.log生成的错误,但仍然无法将其传递回调用的位置,以向用户显示这些错误或将其传递到确认页面。我所得到的似乎都是不明确的。

Metro.call应该看起来像这个

Meteor.call('chargeCard',token,amount,username,function(err,result){
    if(!err){
       Router.go("theRoute") //if there is not error go to the route
     }else{
       console.log(err.reason) // show the error
    }
  })