簡體   English   中英

Meteor.js:方法調用未在客戶端上捕獲錯誤

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

如果在Method中發生任何錯誤,我嘗試在mongoDb中更新文檔時引發用戶定義的錯誤。 我正在調用method並試圖捕獲錯誤,但是我沒有得到一個。 僅在服務器控制台中打印錯誤。 如何在客戶端中捕獲錯誤?

我的代碼示例如下所示:

//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

您的代碼有一些誤解

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返回一個值。

您的代碼有很多錯誤。

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-elements-in-arrays

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM