Javascript Meteor.WrapAsync不返回值
我一直在努力让Meteor.WrapAsync工作,我已经阅读了答案,这段视频,我只是不知道如何从Stripe的电话中回复。我正在使用console.log打印这些步骤,我已经达到了抛出次数4,这意味着,我正在到达stripe服务器并获得响应,但在那之后,我不明白为什么console.log没有打印。如果有人能帮我理解为什么wrapAsyn没有返回条带回调Javascript Meteor.WrapAsync不返回值,javascript,meteor,stripe-payments,Javascript,Meteor,Stripe Payments,我一直在努力让Meteor.WrapAsync工作,我已经阅读了答案,这段视频,我只是不知道如何从Stripe的电话中回复。我正在使用console.log打印这些步骤,我已经达到了抛出次数4,这意味着,我正在到达stripe服务器并获得响应,但在那之后,我不明白为什么console.log没有打印。如果有人能帮我理解为什么wrapAsyn没有返回条带回调 //this functions are part of an anonymous function and running in
//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:
编辑
这就是我如何结束具有条带功能的原因,感谢您的支持
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转换了一个异步函数这意味着一个函数通过同步回调将其结果传输给调用者 传递给Meteor.wrapAsync的函数没有回调作为最终参数,您应该换行_stripe.charge.create 如果要处理错误,则在调用stripe.charge时应使用try/catch块
我看到您正在使用alert记录错误,您是否尝试在客户端上使用Meteor.wrapAsync?Meteor.wrapAsync用于服务器,因为提供同步执行所需的环境在Node.js中可用,而不是在浏览器中。控制台中有错误吗?我猜这是因为您没有将函数绑定到对象:Meteor.wrapAsyncstripe.charge.process、stripe.chargenone错误,一开始我也这么做了,让我现在再试一次……老兄,我不是在开玩笑,现在我发现我自己犯了一个愚蠢的错误,我写了这个答案,但你之前就这么做了,谢谢老兄,你应该得到这个答案。是的,我在服务器端使用它,现在一切都很好,谢谢…你不能得到完整的原始错误对象。我的解决方案贴在这里:这对我帮助很大!谢谢你们!
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;
};
try{
stripe.charge(STRIPE_TOKEN);
}
catch(exception){
console.log("Sorry we couldn't charge the money",exception);
}