【问题标题】:How to return error from Meteor.methods function如何从 Meteor.methods 函数返回错误
【发布时间】:2023-04-09 12:43:01
【问题描述】:

如何从 Meteor.methods 函数返回错误? 我调用我的函数:

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

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

checkCode: function(zcode){
    return Codes.findOne({code: zcode});
  }

谢谢!:)

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以像使用任何普通的 javascript 错误一样使用throw。 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 2021-05-30
      • 1970-01-01
      • 2021-03-07
      • 2017-02-26
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多