2015-10-31 28 views
0

该项目被删除/从数据库中删除,但捕获错误是内部服务器错误。流星:collection.remove。(id)有效,但仍然捕获500错误

事件触发:

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; 
    } 
    } 

}); 

删除()在尝试永远不应该返回,如果有一个陷阱,对不对?

回答

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; 
    } 
    } 

}); 
相关问题