如何从Meteor.methods函数返回错误

yes*_*ode 15 meteor

如何从Meteor.methods函数返回错误?我叫我的功能:

Meteor.call('checkCode', mycode, function(error,result){
    console.log(result['name']);
  })
Run Code Online (Sandbox Code Playgroud)

它返回了我的"代码"与参数相同的人的名字.但如果没有人输入代码,如何返回错误?我的功能代码:

checkCode: function(zcode){
    return Codes.findOne({code: zcode});
  }
Run Code Online (Sandbox Code Playgroud)

谢谢!:)

Aks*_*hat 27

您可以throw像使用任何正常的javascript错误一样使用.Meteor选择它并将错误返回给客户端.

var code = Codes.findOne({code: zcode});

if(!code) 
    throw new Meteor.Error(500, 'Error 500: Not found', 'the document is not found');

return code;
Run Code Online (Sandbox Code Playgroud)