该项目从数据库中删除/删除,但是catch错误是内部服务器错误。
事件触发:
Template.post.events({
'click .delete': function () {
Meteor.call("deleteJob", this._id, function (err, result) {
if (!err) {
console.log("meteor call to remove job was good");
Bert.alert("Poof! Job deleted from the site.", "success");
Router.go('/');
} else {
Bert.alert("Rrrrr. No worky. " + err.reason + " " + result, "danger");
console.log("meteor call was bad", err);
}
});
}
});
方法调用:
Meteor.methods({
deleteJob: function (id) {
var post = Posts.findOne(id);
if (post.author !== Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
try {
var postId = Posts.remove(id);
return postId;
} catch (exception) {
// If an error occurs, return it to the client.
return exception;
}
}
});
如果有一个catch,那么try中的remove()永远不会返回,对吗?
答案 0 :(得分:0)
我正在使用两个检查我的参数的包。 #didntKnow
留意终端窗口,偷看。
我首先在我的方法中需要这个:
check(id, String);
给我这个解决方案:
Meteor.methods({
deleteJob: function (id) {
check(id, String);
var post = Posts.findOne(id);
if (post.author !== Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
try {
var postId = Posts.remove(id);
return postId;
} catch (exception) {
// If an error occurs, return it to the client.
return exception;
}
}
});