gpt4 book ai didi

javascript - Meteor 方法成功更新了数据库,但向 Meteor.call 返回 500 错误

转载 作者:行者123 更新时间:2023-12-03 08:43:47 30 4
gpt4 key购买 nike

我现在正在阅读《Discover Meteor》一书,并尝试使用 Method.call 来编辑帖子(第 8 章),而不是使用更新/删除客户端数据操作。

该调用成功调用了 postModify 方法,并且该方法更新了 Posts 集合。问题在于该方法返回 500 内部服务器错误而不是返回 Meteor.call 的结果。

我认为触发错误的 Posts.updatepostModify 方法中的 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 方法。

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)

感谢您关注这个问题。

最佳答案

当您添加audit-argument-checks包时,您必须检查传递给您的方法和发布者的所有参数。更新您的代码,使其看起来像这样:

Meteor.methods({
postModify: function(updatedPost) {
check(updatedPost, {_id: String, title: String, url: String});
// rest of method goes here

请参阅docs了解更多详细信息和选项。

关于javascript - Meteor 方法成功更新了数据库,但向 Meteor.call 返回 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976267/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com