我正在阅读名为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:
帖子=新的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);
})
}
})
任何想法我做错了什么?
答案 0 :(得分: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}