Meteor.js:在方法调用中未在客户端捕获到错误

Meteor.js: Error not caught on client in method call

本文关键字:客户端 错误 js 方法 调用 Meteor      更新时间:2023-09-26

如果Method中发生任何错误,我将在mongoDb中更新文档时尝试抛出用户定义的错误。我正在调用方法并试图捕捉错误,但没有得到错误。错误仅在服务器控制台中打印。如何在客户端捕获错误?

我的代码示例如下:

//Method
     methodName: (userData) ->
        if(Meteor.isServer and this.userId)
          User.update {_id:this.userId},{$set:{username:userData.username, "profile.name": userData.name ,"emails.$.address": userData.email}}, (error) ->
            if error and error.code == 11000
              throw new Meteor.Error 403, 'Email already in used'


//client Side
        $meteor.call('methodName',result).then ((success) ->
                console.log success // is undefined in both case, when updates and in case of error
                if success
                  console.log 'data updated'
                ), (err) ->
                  console.log err // not entered in this region

您的代码有大量错误。

Meteor.methods({
  methodName: function(userData){
    // you have to create the $set hash programatically first
    var setHash = {
      "$set": {
        "username": userData.username,
        "profile.name": userData.name,
        // what's going on here with the dollar sign you originally had?
        "emails.0.address": userData.email
      }
    };
    if( Meteor.isServer && this.userId() ){
      // It's Users, not User
      return Users.update( { _id: this.userId() }, setHash, function(error, numberOfAffectedDocuments){
        if(error && error.code == "11000"){
          // Error names have to be a string
          throw new Meteor.error("403", "Email already in use.");
        } else {
          return "Success! The number of affected documents is " + numberOfAffectedDocuments;
      });
    };
  }
});
// in your original code you never sent your Meteor Method any arguments when calling it
Meteor.call('methodName', userDataObject, function(error, result){
  if(error){
    console.log("there was an error: ", error);
  } else {
    console.log("method call was a success: ", result);
  };
});

参考文献:

http://docs.mongodb.org/manual/reference/operator/update/set/#set-阵列中的元素

http://docs.meteor.com/#/full/meteor_user

您的代码对有几个误解

1) 方法是一个同步调用。这意味着,如果它在引发错误之前完全返回或运行,则不会在客户端上调用错误回调。

这意味着您需要始终使用同步代码。目前,您正在使用回调。

您可以使用这种方法:

Meteor.methods methodName: (userData) ->
  if Meteor.isServer and @userId
    return User.update({ _id: @userId }, $set:
      username: userData.username
      'profile.name': userData.name
      'emails.$.address': userData.email)

这将引发一个错误,客户端将收到该错误,原因为"内部服务器错误"。作为一个"包罗万象"的错误。代码之间的区别在于没有回调。

使用try..catch模式,您可以使用此语法捕获特定的重复密钥错误。

2) User.update {_id:this.userId}始终运行。更新文档时,您正在查找"重复密钥"11000错误。这不是最好的方法。你应该事先直接查看电子邮件。

3) 一个方法应该return一个值。目前,你什么都不回。对于结果,只能使用一个回调或检查方法返回的值。目前,您同时执行这两项操作,因此User.update的结果为undefined。这就是您看到undefined的原因。这应该有效:

Meteor.methods methodName: (userData) ->
  if Meteor.isServer and @userId
    if(User.emails.address':userData.email})) throw new Meteor.Error(500, "Email already exists");
    return User.update({ _id: @userId }, $set:
      username: userData.username
      'profile.name': userData.name
      'emails.$.address': userData.email)
  else return false

所以在这里,你可以直接检查是否有用户使用了电子邮件,并抛出错误&如果未使用,请更新它。没有回调,因此它应该向客户端上的Meteor.call返回一个值。