我的流星代码在某些方面深入探讨了几种Meteor.call方法。如果我在第二层有错误,我想将流星错误扔回客户端,我该怎么办呢?
目前我有这样的事情,但我的输出非常混乱,我不认为我完全理解当我打电话给throw new Meteor.Error(500, e.category_code, e.description);
在client.js
中Meteor.call('firstCall', data, function (error, result) {
if(result) {
doSomething();
}
else{
console.log(error);//just shows 500
}
});
在server.js
中var Future = Meteor.npmRequire("fibers/future");
function extractFromPromise(promise) {
var fut = new Future();
promise.then(function (result) {
fut.return(result);
}, function (error) {
console.log(error);
fut.throw(error);
});
return fut.wait();
}
firstCall: function (data){
try{
Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test' function (error, result) {
return result;
});
}
catch(e){
throw new Meteor.Error(500, e.category_code, e.description);
}
}
secondCall: function (data, paymentHref, otherHref){
try{
var associate = extractFromPromise(balanced.get(paymentHref).associate_to_customer(otherHref).debit({
"amount": data.paymentInformation[0].total_amount * 100,
"appears_on_statement_as": "Trash Mountain"}));
}
catch(e){
Collection.update(data.id, {
$set: {
'failed.category_code': e.category_code,
'failed.description': e.description
}
});
throw new Meteor.Error(500, e.category_code, e.description);
}
}
答案 0 :(得分:2)
在您的情况下,当firstCall
抛出时,e.category_code
中的捕获不会为e.description
和secondCall
定义任何内容。这是因为在secondCall
中您将这两个作为参数传递给Meteor.Error
,其作为参数error
,reason
和details
:
https://github.com/meteor/meteor/blob/devel/packages/meteor/errors.js
为了传递这些内容,您需要修改firstCall
以使用这些属性:
firstCall: function (data){
try{
Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test');
}
catch(e){
throw new Meteor.Error(500, e.reason, e.details);
}
}
我甚至不确定你需要将它分成两个模块化调用,因为你可以使用普通的Javascript函数。但我们可以在别处讨论。
答案 1 :(得分:0)
我在这里提几件事:
Meteor._wrapAsync
进行同步,我将在后面解释),它们以另一种方式返回错误(作为NodeJS回调式的第一个参数) )。这适用于Meteor.call
和doSomeAsyncThing
。Meteor.call
的好处。 Meteor.call
用于从客户端调用服务器方法。在这种情况下,您只需从YourObj.secondCall
内部拨打firstCall
。firstCall
内所做的那样)并没有任何效果。您希望异步代码作为同步代码使用,因此我建议使用Meteor._wrapAsync
,这是非常好的解释here。
所以,我将实现服务器端有点不同:firstCall: function (data){
try{
return this.secondCall(data);
}
catch(e){
throw new Meteor.Error(500, e.category_code, e.description);
}
secondCall: function (data){
try{
return Meteor._wrapAsync(doSomeAsyncThing)(data);
}
catch(e){
Collection.update(data.id, {
$set: {
'failed.category_code': e.category_code,
'failed.description': e.description
}
});
throw new Meteor.Error(500, e.category_code, e.description);
}
希望这有帮助!