【问题标题】:Meteor.WrapAsync don't return valueMeteor.WrapAsync 不返回值
【发布时间】:2014-12-06 23:49:15
【问题描述】:

我一直在努力工作Meteor.WrapAsync 我已经阅读了Meteor wrapAsync syntax 的答案,这个视频https://www.eventedmind.com/feed/meteor-meteor-wrapasync 我只是不知道如何return 来自 Stripe 的电话的回应。我正在使用console.log 打印步骤,并且我已经达到第 4 次抛出,这意味着,我到达stripe 服务器并获得响应,但之后我看不出为什么console.log(5) 它没有打印。如果有人可以帮助我理解为什么它的 wrapAsyn 没有返回条带回调?

    //this functions are part of an anonymous function and running in the server side of meteor
    stripe.charge = function (stripeToken) {
        // get a sync version of our API async func
        var strypeChargeSync = Meteor.wrapAsync(stripe.charge.process);

        // call the sync version of our API func with the parameters from the method call

        console.log("1");

        var response = strypeChargeSync(stripeToken);

        console.log("5" + response); ///// this never get print / log
        return response;
    }

    stripe.charge.process = function(stripeToken){
        var _stripe = StripeAPI(stripeKey);
        console.log("2");
        var charge = _stripe.charges.create({
            amount: 1000, // amount in cents, again
            currency: "cad",
            card: stripeToken.id,
            description: "paid@whatever"
        }, function(err, charge) {
            if (err && err.type === 'StripeCardError') {
                alert("Sorry we couldn't charge the money: " + err);
                //console.log(err);
            }else{
                console.log("4");
                //console.log(charge);
                return charge;
            }
        });
        console.log("3");
    }

//当前输出 1,2,3,4 但绝不是 5 :(

编辑

感谢支持,这就是我结束 Stripe 功能的方式

    var syncFunction = Meteor.wrapAsync(_stripe.charges.create, _stripe.charges);
    var response = syncFunction({
        amount: 1000, // amount in cents, again
        currency: "cad",
        card: stripeToken.id,
        description: "paid@whatever"
    });

【问题讨论】:

  • 控制台有错误吗?我猜这是因为您没有将函数绑定到对象:Meteor.wrapAsync(stripe.charge.process, stripe.charge)
  • 没有错误,一开始也是这样做的,让我现在再试一次...

标签: javascript meteor stripe-payments


【解决方案1】:

您在这里包装了错误的函数,Meteor.wrapAsync 将异步函数(这意味着通过回调将其结果传输给调用者的函数)转换为同步函数。

您传递给Meteor.wrapAsync 的函数没有回调作为最终参数,您应该将_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;
};

如果你想处理错误,你应该在调用stripe.charge时使用try/catch块。

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

我看到您正在使用alert 记录您的错误,您是否尝试在客户端上使用Meteor.wrapAsyncMeteor.wrapAsync 应在服务器上使用,因为 Node.js 中提供了提供同步执行所需的环境,而不是浏览器。

【讨论】:

  • 伙计,我现在不是在开玩笑,我自己发现了这个愚蠢的错误,我要写这个答案,但是你在谢谢你之前就做到了,你应该得到答案。这将在未来对某些机构有很大帮助。
  • 是的,我在服务器端使用它,现在一切正常,谢谢...
  • 你无法获得完整的原始错误对象。我的解决方案贴在这里:github.com/meteor/meteor/issues/2774
  • 这对我帮助很大!谢谢你们!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2016-02-22
相关资源
最近更新 更多