Meteor:在 Meteor.method 中调用异步函数并返回结果 [英] Meteor: Calling an asynchronous function inside a Meteor.method and returning the result
本文介绍了Meteor:在 Meteor.method 中调用异步函数并返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Meteor 方法中调用一个异步函数,然后将该函数的结果返回给 Meteor.call.
(怎么)这可能?
Meteor.methods({我的功能:功能(arg1,arg2){//调用其他异步函数并返回结果或抛出错误}});
解决方案
Andrew Mao 是对的.Meteor 现在有 Meteor.wrapAsync() 用于这种情况.
这是通过条带进行充电并传递回调函数的最简单方法:
var stripe = StripeAPI("key");流星.方法({你的方法:函数(callArg){var charge = Meteor.wrapAsync(stripe.charges.create, stripe.charges);收费({金额:金额,货币:美元",//我在callArg中传递了stripe token卡:callArg.stripeToken,},功能(错误,收费){if (err && err.type === 'StripeCardError') {//该卡已被拒绝throw new Meteor.Error("stripe-charge-error", err.message);}//在此处插入您的成功"代码});}});
我发现这篇文章真的很有帮助:Meteor:在服务器上正确使用 Meteor.wrapAsync>
I want to call an asynchronous function inside a Meteor method and then return the result from that function to Meteor.call.
(How) is that possible?
Meteor.methods({
my_function: function(arg1, arg2) {
//Call other asynchronous function and return result or throw error
}
});
解决方案
Andrew Mao is right. Meteor now has Meteor.wrapAsync() for this kind of situation.
Here's the simplest way to do a charge via stripe and also pass a callback function:
var stripe = StripeAPI("key");
Meteor.methods({
yourMethod: function(callArg) {
var charge = Meteor.wrapAsync(stripe.charges.create, stripe.charges);
charge({
amount: amount,
currency: "usd",
//I passed the stripe token in callArg
card: callArg.stripeToken,
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
throw new Meteor.Error("stripe-charge-error", err.message);
}
//Insert your 'on success' code here
});
}
});
I found this post really helpful: Meteor: Proper use of Meteor.wrapAsync on server
这篇关于Meteor:在 Meteor.method 中调用异步函数并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文