【问题标题】:Meteor method successfully updated the DB but returns 500 error to Meteor.callMeteor 方法成功更新了数据库,但返回 500 错误给 Meteor.call
【发布时间】:2016-01-03 17:49:30
【问题描述】:

我现在正在阅读 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)

感谢您查看此问题。

【问题讨论】:

  • 一个错误应该被打印到你的 shell 中,更详细地描述服务器上发生的事情 - 这说明了什么?
  • collection.update(selector, modifier, [options], [callback]) 返回受影响文档的数量。因此,我认为您的 modifiedPost 变量不包含更新后的文档。
  • @DavidWeldon 用日志更新了 OP
  • @MatthiasEckhart 是的,我认为你是对的。当我 console.log'd modifiedPost 它返回 1
  • @Vitiell0 我知道。您能否在更新操作之前将check(updatedPost, Object); 添加到您的 Meteor 方法中?

标签: javascript mongodb meteor


【解决方案1】:

当您添加audit-argument-checks 包时,您必须将check 传递给您的方法和发布者的所有参数。将您的代码更新为如下所示:

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

有关更多详细信息和选项,请参阅docs

【讨论】:

  • 谢谢!这就是我所缺少的。在我进行更改后,您建议我返回 1(正如@Matthias 指出的那样),但路由器正在工作。所以我改变了返回 currentPostId 而不是 modifiedPost 的方法,现在它正在工作。
猜你喜欢
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多