【问题标题】:Meteor.js Discover Meteor Book Error While Simulating Effect OfMeteor.js 在模拟效果时发现 Meteor Book 错误
【发布时间】:2014-08-15 01:03:44
【问题描述】:

我正在阅读名为 Discover Meteor 的 Meteor 书,我在第 7 章第 6 节中。我正在尝试使用 Meteor.methods 为 Web 应用程序创建发布功能。当我点击提交按钮时,出现以下错误:

即,

http://localhost:3000 Internal server error

JS 控制台:

[Log] Exception while simulating the effect of invoking 'post'  (meteor.js, line 733)
Error
line: 24
message: "'undefined' is not a function (evaluating '_.post(postAttributes, 'url', 'title', 'message')')"
sourceURL: "http://localhost:3000/collections/posts.js?fc84533d5acd5a26ddd07233ffdf1c6d50eb03ae"
stack: "post@http://localhost:3000/collections/posts.js?fc84533d5acd5a26ddd07233ffdf1c6d50eb03ae:24:35↵http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4140:30↵withValue@http://localhost:3000/packages/meteor.js?7a66be7a03504cd2c18dd47b699e6233b60675ed:793:21↵apply@http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4131:63↵call@http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4021:22↵[native code]↵submit form@http://localhost:3000/client/views/posts/post_submit.js?5b0a8174d424b90c1cf9682a03b94dcfe2985f85:10:20↵http://localhost:3000/packages/templating.js?e2c0d3bbe4292a0b20c3083eaf4fcd0f5f91bb52:120:23↵http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2205:35↵withCurrentView@http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2038:16↵http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2204:41↵http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:802:29↵dispatch@http://localhost:3000/packages/jquery.js?265926494aaa3929cd2e30da265211c5929f37a4:4657:14↵handle@http://localhost:3000/packages/jquery.js?265926494aaa3929cd2e30da265211c5929f37a4:4325:33"
__proto__: Error
post@http://localhost:3000/collections/posts.js?fc84533d5acd5a26ddd07233ffdf1c6d50eb03ae:24:35
http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4140:30
withValue@http://localhost:3000/packages/meteor.js?7a66be7a03504cd2c18dd47b699e6233b60675ed:793:21
apply@http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4131:63
call@http://localhost:3000/packages/livedata.js?edca5d3826d0b2bd3507cac956e98d83559da4d8:4021:22
[native code]
submit form@http://localhost:3000/client/views/posts/post_submit.js?5b0a8174d424b90c1cf9682a03b94dcfe2985f85:10:20
http://localhost:3000/packages/templating.js?e2c0d3bbe4292a0b20c3083eaf4fcd0f5f91bb52:120:23
http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2205:35
withCurrentView@http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2038:16
http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:2204:41
http://localhost:3000/packages/blaze.js?309c2a3b573dca998c07c493ba4953d451b2c963:802:29
dispatch@http://localhost:3000/packages/jquery.js?265926494aaa3929cd2e30da265211c5929f37a4:4657:14
handle@http://localhost:3000/packages/jquery.js?265926494aaa3929cd2e30da265211c5929f37a4:4325:33

我的 router.js:

Posts = new Meteor.Collection('posts');

Meteor.methods({
    post: function(postAttributes) {
        var user = Meteor.user(),
            postWithSameLink = Posts.findOne({url: postAttributes.url});

        // user is not logged in
        if(!user) {
            throw new Meteor.error(401, "You need to login to post new stories.")
        };

        // no title
        if(!postAttributes.title) {
            throw new Meteor.error(422, "Please fill in a headline.");
        };

        // no posts with same link
        if(postAttributes.url && postWithSameLink) {
            throw new Meteor.error(302, "This link has already been posted.", postWithSameLink._id);
        };

        // pick whitelisted keys
        var post = _.extend(_.post(postAttributes, 'url', 'title', 'message'), [{
            userId: user._id,
            author: user.username,
            submitted: new Date().getTime()

        }]);

        var postId = Posts.insert(post);
        return postId;
    }
})

post_submit.js:

Template.postSubmit.events({
    'submit form': function(e) {
        e.preventDefault();
        var post = {
            url: $(e.target).find('[name=url]').val(),
            title: $(e.target).find('[name=title]').val(),
            message: $(e.target).find('[name=message]').val()
        }

        Meteor.call('post', post, function(error) {
            if (error) {
                return alert(error.reason);
            }
            Router.go('postPage', post);
        })

    }

})

任何想法我做错了什么?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    你有一个错字。

    代替

    var post = _.extend(_.post(postAttributes, 'url', 'title', 'message'), [{
      userId: user._id,
      author: user.username,
      submitted: new Date().getTime()
    }]);
    

    你应该输入

    var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), [{
      userId: user._id,
      author: user.username,
      submitted: new Date().getTime()
    }]);
    

    注意使用_.pick 而不是_.post,其中_.pick(object, *keys)underscorejs object utility,它返回对象的副本,过滤后仅包含白名单键(或有效键数组)的值)。

    _.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
    => {name: 'moe', age: 50}
    

    【讨论】:

    • 这消除了最初的错误,但我现在得到了另一个:
    • 经过更多测试,我发现错误抛出有效,并且它添加了帖子,只是没有重定向到那里。任何想法为什么会这样?
    • 看起来新问题在 post_submit.js 中的某个地方,但这是另一个问题。如果我的回答回答了您的问题,那么您应该接受它,发布一个包含新信息的新问题。不要包含 imgur 链接,粘贴在屏幕截图本身中,更好的是,复制并粘贴错误。另外,我会确保不再有任何错别字。您可以将您的代码与显微镜的 github 存储库进行比较。
    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2015-08-28
    • 2016-09-23
    相关资源
    最近更新 更多