简体   繁体   中英

Meteor method successfully updated the DB but returns 500 error to Meteor.call

I am working through the Discover Meteor book right now and am attempting to use Method.call to edit posts (chapter 8) instead of using update/remove client side data manipulation.

The call successfully calls the postModify Method and the method updates the Posts collection. The problem is that the Method is returning a 500 internal server error instead of a result back to Meteor.call.

I think there is either something wrong with Posts.update or the return statement inside the postModify method that is triggering the error.

This is the call.

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

This is the postModify Meteor method.

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

This is the route:

Router.route('/posts/:_id', {
    name: 'postPage',
    data: function() {
        return Posts.findOne(this.params._id); }
});

This is the message from the 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\"}}"]

The next line shows that the collection was successfully updated:

a["{\"msg\":\"changed\",\"collection\":\"posts\",\"id\":\"apT8KEC7z8ci3TFD4\",\"fields\":{\"title\":\"Daniel is1\"}}"]

Here is the log from the 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)

Thank you for taking a look at this problem.

When you add the audit-argument-checks package, you have to check all of the arguments passed to your methods and publishers. Update your code to look something like this:

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

See the docs for more details and options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM