如何从另一个Meteor.call中的Meteor.call方法返回错误

Jos*_*Joe 5 meteor

我的流星代码在某些方面深入了解了几个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
  }
});
Run Code Online (Sandbox Code Playgroud)

在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);
  }
}
Run Code Online (Sandbox Code Playgroud)

And*_*Mao 2

在您的情况下,捕获firstCall不会为抛出e.category_codee.description抛出定义任何内容secondCall。这是因为secondCall您将这两个作为参数传递给Meteor.Error,它以errorreason和为参数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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我什至不确定您是否需要将其分成两个调用以实现模块化,因为您可以只使用普通的 Javascript 函数。但我们可以在其他地方讨论这个问题。