Meteor.WrapAsync不返回值 [英] Meteor.WrapAsync don't return value

查看:71
本文介绍了Meteor.WrapAsync不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力工作 Meteor.WrapAsync 我已阅读流星wrapAsync语法回答,此视频 https://www.eventedmind.com/feed/meteor-meteor-wrapasync ,我只是想不通如何从Stripe的呼叫中返回的响应.我正在使用 console.log 打印步骤,并且我一直抛出第4号,这意味着,我正在到达 stripe 服务器并获得响应,但是在此之后,我可以没有看到为什么 console.log(5)无法打印.如果有人可以帮助我理解为什么它的wrapAsyn不返回条纹回调的话?

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 I just cant figure how to return the response from the call from Stripe. I'm using console.log to print the steps, and I been reaching throw number 4 which mean, Im reaching stripe server and getting the response, but after that I can't see why console.log(5) its not printing. please if somebody can help me to understand why its the wrapAsyn its not returning the stripe callback?

    //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:(

//current output 1,2,3,4 but never 5 :(

编辑

这是我结束拥有Stripe功能的方式,谢谢支持

this is how I end having the Stripe function thanks for the support

    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 转换了一个异步函数(这意味着一个将其结果传输到调用者通过回调).

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.

传递给 Meteor.wrapAsync 的函数没有作为最终参数的回调,而应包装 _stripe.charge.create .

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;
};

如果要处理错误,则在调用 stripe.charge 时应使用 try / catch 块.

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);
}

我看到您正在使用 alert 记录错误,是否要在客户端上使用 Meteor.wrapAsync ? Meteor.wrapAsync 打算在服务器上使用,因为提供同步外观执行所需的环境在Node.js中可用,而不是在浏览器中可用.

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.

这篇关于Meteor.WrapAsync不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆