简体   繁体   English

流星在调用服务器端方法中的错误句柄

[英]Error handle in Meteor in calling server side method

I call a method for deleting family from server/publicationMehods like this: 我调用了一种从服务器/ publicationMehods删除家庭的方法,如下所示:

    deletedFamily(family) {
      if (Meteor.user().roles[0] == "admin") {



         var myUsers = Meteor.users.find({"profile.family_id": family._id}).fetch();

         for (var i = 0; i < myUsers.length; i++) {
           UsersDeleted.insert(myUsers[i]);
           Meteor.users.remove(myUsers[i]);
          }

         var myGuests= Guests.find({"profile.family_id": family._id}).fetch();

         for (var i = 0; i < myGuests.length; i++) {
           GuestsDeleted.insert(myGuests[i]);
           Guests.remove(myGuests[i]);
          }

         FamiliesDeleted.insert(family);
         Families.remove(family);

      }
}

I want to handle exception and catch it if any errors happend and in frond-end show the result. 我想处理异常并在发生任何错误时捕获它,并在前端显示结果。 I know there is not any transaction in Meteor. 我知道流星上没有任何交易。 But I need to show the result to user at least. 但是我至少需要向用户显示结果。

You can use throw/catch. 您可以使用掷/接球。

Read the following document: 阅读以下文档:

Throw

In Meteor, if you want to return an error to a user from a Meteor method then you throw an exception, but it must be a Meteor.Error exception object in order to send it back to the client. 在Meteor中,如果要从Meteor方法向用户返回错误,则抛出异常,但必须是Meteor.Error异常对象才能将其发送回客户端。

On the client side, when you call a Meteor method on the server, you provide a callback function that receives an error and result. 在客户端,当您在服务器上调用Meteor方法时,您将提供一个接收错误和结果的回调函数。 If you wish to display an error to the user then whatever Meteor.Error exception object that was thrown in the method will be in the error callback argument. 如果希望向用户显示错误,则该方法中引发的任何Meteor.Error异常对象都将位于error回调参数中。

Here is an example. 这是一个例子。 First let's look at the meteor method throwing an exception. 首先让我们看一下抛出异常的流星方法。

Meteor.methods({
  deletedFamily: function(family) {
    //... your logic here...

    if (somethingWentWrong) {
      throw new Meteor.Error("logged-out", "The user must be logged in to delete a family.");
    } else {
      return // something
    }
  },
});

On the client, you would call the method like this and if an error was thrown it will be in the error object. 在客户端上,您将这样调用方法,如果引发了错误,它将在error对象中。

// on the client
Meteor.call("deletedFamily", function (error, result) {
  // identify the error
  if (error && error.error === "logged-out") {
    // show a nice error message
    Session.set("errorMessage", "Please log in to delete a family.");
  }

  //...continue on with your logic...
});

If you need to pass along an exception generated by something else (mongodb for example), then just use try/catch blocks and pass a Meteor.Error when needed. 如果您需要传递其他事物(例如mongodb)生成的异常,则只需使用try/catch块并在需要时传递Meteor.Error Here is an example. 这是一个例子。

Meteor.methods({
  deletedFamily: function(family) {
    //... your logic here...

    try {
      // Mongodb insert or update
    } catch(e) {
      if (e instanceof WriteError && e.code === '11000') {
        throw new Meteor.Error("duplicate-error", "The family already exists.");
      }
    }
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM