使用Meteor和异步平衡支付功能
我在Meteor中使用的是平衡支付及其balanced.js的1.1版 我正在尝试使用使用Meteor和异步平衡支付功能,meteor,balanced-payments,Meteor,Balanced Payments,我在Meteor中使用的是平衡支付及其balanced.js的1.1版 我正在尝试使用 balanced.marketplace.customers.create(formData) 这是我的CheckFormSubmitEvents.js文件 Template.CheckFormSubmit.events({ 'submit form': function (e, tmpl) { e.preventDefault(); var recurringStat
balanced.marketplace.customers.create(formData)代码>
这是我的CheckFormSubmitEvents.js文件
Template.CheckFormSubmit.events({
'submit form': function (e, tmpl) {
e.preventDefault();
var recurringStatus = $(e.target).find('[name=is_recurring]').is(':checked');
var checkForm = {
name: $(e.target).find('[name=name]').val(),
account_number: $(e.target).find('[name=account_number]').val(),
routing_number: $(e.target).find('[name=routing_number]').val(),
recurring: { is_recurring: recurringStatus },
created_at: new Date
}
checkForm._id = Donations.insert(checkForm);
Meteor.call("balancedCardCreate", checkForm, function(error, result) {
console.log(result);
// Successful tokenization
if(result.status_code === 201 && result.href) {
// Send to your backend
jQuery.post(responseTarget, {
uri: result.href
}, function(r) {
// Check your backend result
if(r.status === 201) {
// Your successful logic here from backend
} else {
// Your failure logic here from backend
}
});
} else {
// Failed to tokenize, your error logic here
}
// Debuging, just displays the tokenization result in a pretty div
$('#response .panel-body pre').html(JSON.stringify(result, false, 4));
$('#response').slideDown(300);
});
}
});
这是我的Methods.js文件
var wrappedDelayedFunction = Async.wrap(balanced.marketplace.customers.create);
Meteor.methods({
balancedCardCreate: function (formData) {
console.log(formData);
var response = wrappedDelayedFunction(formData);
console.log(response);
return response;
}
});
当我提交表单时,除了在服务器控制台上看到表单数据的日志之外,我什么也没有得到
我肯定我没有正确调用这些异步函数。这里对我来说最困难的部分是平衡函数是异步的,但我不知道它们是否与我所看到的一些示例适用于同一个模型
我已经试着遵循这个示例代码。
在与balanced合作方面是否需要做具体的改变?有没有人对使用异步函数有什么建议,或者看到我的代码中某些我做错了的地方
感谢NPM实用程序Async.wrap
与未记录的Meteor函数Meteor做了相同的事情。\u wrapAsync
,它接受一个带有最后一个参数函数(err,result){}
的异步函数,并将其转换为具有相同参数的同步函数,但返回结果或抛出错误,而不是使用回调。在异步回调返回之前,函数将在光纤中生成,以便事件循环中的其他代码可以运行
这样做的一个缺陷是,您需要确保使用正确的上下文调用您包装的函数。因此,如果balanced.marketplace.customers.create
是一个原型方法,它期望将此
设置为某个值,那么除非您自己使用函数.bind
或其他各种库函数绑定它,否则将无法正确设置它
有关更多信息,请参阅。我最后做的是使用未来。这非常有效,我只需要在捕捉错误方面做得更好。我想这将是一个专业人士的问题;-)
应该感谢用户3374348回答了我的另一个类似问题,解决了这两个问题。
正如Andrew提到的,您需要为该方法设置上下文
下面是使用Async.wrap
Async.wrap(balanced.marketplace.customers, "create");
当我使用这个函数时,该函数似乎永远不会从服务器调用返回。这就像异步函数没有传递到下一步一样好的,balanced有一个promise API。这是行不通的。您需要直接使用futures或Async.runSyncOk,在运行Async.runSync之前,我是否仍需要为该方法设置上下文?您有更多的Aysnc.runSync示例吗?我现在正在尝试使用它,但是我得到了未定义的数据,我仍然不相信我的设置正确。
Async.wrap(balanced.marketplace.customers, "create");