admin管理员组文章数量:1023656
I call a method for deleting family from server/publicationMehods like this:
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.
I call a method for deleting family from server/publicationMehods like this:
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.
Share Improve this question asked Mar 28, 2017 at 3:43 MehrnooshMehrnoosh 8992 gold badges12 silver badges27 bronze badges2 Answers
Reset to default 4In 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.
On the client side, when you call a Meteor method on the server, you provide a callback function that receives an error and result. 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.
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.
// 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. 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.");
}
}
},
});
You can use throw/catch.
Read the following document:
Throw
I call a method for deleting family from server/publicationMehods like this:
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.
I call a method for deleting family from server/publicationMehods like this:
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.
Share Improve this question asked Mar 28, 2017 at 3:43 MehrnooshMehrnoosh 8992 gold badges12 silver badges27 bronze badges2 Answers
Reset to default 4In 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.
On the client side, when you call a Meteor method on the server, you provide a callback function that receives an error and result. 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.
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.
// 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. 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.");
}
}
},
});
You can use throw/catch.
Read the following document:
Throw
本文标签: javascriptError handle in Meteor in calling server side methodStack Overflow
版权声明:本文标题:javascript - Error handle in Meteor in calling server side method - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745527352a2154587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论