我正在研究Discover Meteor的书,我正在尝试使用Method.call来编辑帖子(第8章),而不是使用更新/删除客户端数据操作。
调用成功调用postModify
方法,该方法更新Posts
集合。问题是该方法返回500内部服务器错误而不是返回Meteor.call的结果。
我认为Posts.update
或者postModify
方法中的return语句触发了错误。
这是电话。
Template.postEdit.events({
'submit form': function(e) {
e.preventDefault();
var postProperties = {
url: $(e.target).find('[name=url]').val(),
title: $(e.target).find('[name=title]').val(),
_id: this._id
};
Meteor.call('postModify', postProperties, function (error, result) {
if (error)
return alert(error.reason);
Router.go('postPage', {_id: result._id});
}); } });
这是postModify
流星方法。
Meteor.methods({
postModify: function(updatedPost) {
var currentPostId = updatedPost._id;
var newTitle = updatedPost.title;
var newUrl = updatedPost.url;
var modifiedPost = Posts.update({_id: currentPostId}, {$set: {
title: newTitle,
url: newUrl
}});
console.log("the post has been updated");
return {
_id: modifiedPost
}; } });
这是路线:
Router.route('/posts/:_id', {
name: 'postPage',
data: function() {
return Posts.findOne(this.params._id); }
});
这是来自websocket的消息:
["{\"msg\":\"method\",\"method\":\"postModify\",\"params\":[{\"url\":\" testing.com\",\"title\":\"Daniel is1\",\"_id\":\"apT8KEC7z8ci3TFD4\"}],\"id\":\"2\"}"]
a["{\"msg\":\"result\",\"id\":\"2\",\"error\":{\"error\":500,\"reason\":\"Internal server error\",\"message\":\"Internal server error [500]\",\"errorType\":\"Meteor.Error\"}}"]
下一行显示该集合已成功更新:
a["{\"msg\":\"changed\",\"collection\":\"posts\",\"id\":\"apT8KEC7z8ci3TFD4\",\"fields\":{\"title\":\"Daniel is1\"}}"]
以下是shell的日志:
I20151006-10:27:14.098(-7)? the post has been updated
I20151006-10:27:14.099(-7)? { isSimulation: false,
I20151006-10:27:14.099(-7)? _unblock: [Function],
I20151006-10:27:14.099(-7)? _calledUnblock: false,
I20151006-10:27:14.099(-7)? userId: 'cLzs8ixMfgXeHLXLc',
I20151006-10:27:14.099(-7)? _setUserId: [Function],
I20151006-10:27:14.100(-7)? connection:
I20151006-10:27:14.100(-7)? { id: 'bXSjdEXHC3utPYMDn',
I20151006-10:27:14.100(-7)? close: [Function],
I20151006-10:27:14.100(-7)? onClose: [Function],
I20151006-10:27:14.100(-7)? clientAddress: '127.0.0.1',
I20151006-10:27:14.100(-7)? httpHeaders:
I20151006-10:27:14.100(-7)? { 'x-forwarded-for': '127.0.0.1',
I20151006-10:27:14.101(-7)? host: 'localhost:3000',
I20151006-10:27:14.101(-7)? 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
I20151006-10:27:14.101(-7)? 'accept-language': 'en-US,en;q=0.8' } },
I20151006-10:27:14.101(-7)? randomSeed: null,
I20151006-10:27:14.101(-7)? randomStream: null } 'apT8KEC7z8ci3TFD4' 'Daniel is1' ' testing.com' 1
I20151006-10:27:14.109(-7)? Exception while invoking method 'postModify' Error: Did not check() all arguments during call to 'postModify'
I20151006-10:27:14.109(-7)? at [object Object]._.extend.throwUnlessAllArgumentsHaveBeenChecked (packages/check/packages/check.js:365:1)
I20151006-10:27:14.110(-7)? at Object.Match._failIfArgumentsAreNotAllChecked (packages/check/packages/check.js:120:1)
I20151006-10:27:14.110(-7)? at maybeAuditArgumentChecks (livedata_server.js:1689:18)
I20151006-10:27:14.110(-7)? at livedata_server.js:708:19
I20151006-10:27:14.110(-7)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20151006-10:27:14.110(-7)? at livedata_server.js:706:40
I20151006-10:27:14.111(-7)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20151006-10:27:14.111(-7)? at livedata_server.js:704:46
I20151006-10:27:14.111(-7)? at tryCallTwo (/Users/danielvitiello/.meteor/packages/promise/.0.5.0.97m7hz++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:45:5)
I20151006-10:27:14.111(-7)? at doResolve (/Users/danielvitiello/.meteor/packages/promise/.0.5.0.97m7hz++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:171:13)
感谢您查看此问题。
答案 0 :(得分:1)
添加audit-argument-checks
包时,您必须check
传递给您的方法和发布商的所有参数。更新您的代码看起来像这样:
Meteor.methods({
postModify: function(updatedPost) {
check(updatedPost, {_id: String, title: String, url: String});
// rest of method goes here
有关详细信息和选项,请参阅docs。