如果我尝试添加评论或喜欢配方,我会收到错误错误调用方法' addReview':内部服务器错误[500] debug.js:41 ,即使它将评论和喜欢添加到数据库中并且工作正常,但仍然让我犯了错误。
源代码Github
add_review.js
Meteor.methods({
addReview:function(rating,review,recipeId){
if(review!=""){
Reviews.insert({
rating:rating,
review:review,
recipeId:recipeId
});
Router.go('reviews',{_id:recipeId});
FlashMessages.sendSuccess('Review Added',{ autoHide: true, hideDelay: 2000 });
}
else{
FlashMessages.sendError('Review field is empty',{ autoHide: true, hideDelay: 3000 });
}
return false;
},
upvote:function(currentRecipe){
var user = Meteor.user();
if(!user){
FlashMessages.sendError("You need to login to like this recipe", {hideDelay: 1000});
}
if (currentRecipe) {
if (_.contains(currentRecipe.voters, Meteor.userId())) {
FlashMessages.sendError("You already liked this recipe", {hideDelay: 1000});
return false;
}
Recipes.update(currentRecipe._id, {$addToSet: {voters: Meteor.userId()}, $inc: {likes: 1}});
}
}
})
的客户机/ methods.js
RecipesImages.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc, fieldNames, modifier) {
return true;
},
remove: function(userId, doc) {
return false;
},
download: function(userId,doc) {
return true;
},
fetch: null
});
Recipes.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc, fieldNames, modifier) {
return true;
}
});
Reviews.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc, fieldNames, modifier) {
return true;
}
});
服务器/ permissions.js
let viewController: SomeViewController = self.dequeueReusableViewController()
答案 0 :(得分:1)
您似乎正在尝试使用不需要的方法,只会让事情变得更难。这不仅仅是客户端代码吗?如果是这样,你可以使用函数:
<强> add_review.js 强>
Template.add_review.events({
'submit .add-review':function(event){
event.preventDefault();
var rating = event.target.rating.value;
var review = event.target.review.value;
var recipeId = Router.current().data()._id;
addReview(rating,review,recipeId);
}
});
Template.recipes.events({
"click [data-action='addLikes']": function (event) {
event.preventDefault();
var recipe = Recipes.findOne({_id: this._id});
upvote(recipe)
}
});
<强>的客户机/ methods.js 强>
addReview = function(rating,review,recipeId){
if(review!=""){
Reviews.insert({
rating:rating,
review:review,
recipeId:recipeId
});
Router.go('reviews',{_id:recipeId});
FlashMessages.sendSuccess('Review Added',{ autoHide: true, hideDelay: 2000 });
}
else{
FlashMessages.sendError('Review field is empty',{ autoHide: true, hideDelay: 3000 });
}
return false;
};
upvote = function(currentRecipe){
var user = Meteor.user();
if(!user){
FlashMessages.sendError("You need to login to like this recipe", {hideDelay: 1000});
}
if (currentRecipe) {
if (_.contains(currentRecipe.voters, Meteor.userId())) {
FlashMessages.sendError("You already liked this recipe", {hideDelay: 1000});
return false;
}
Recipes.update(currentRecipe._id, {$addToSet: {voters: Meteor.userId()}, $inc: {likes: 1}});
}
};
答案 1 :(得分:1)
@Waqar首先,在您的methods.js文件中,就像其他人已经告诉过你应该进入/ lib文件夹一样,你应该在你的函数名称周围加上单引号。我已经为Meteor.methods和下面的Template.add-review.events重新编写了代码
client / templates / reviews / add_review.js(抱歉,我把每个模板和js文件放在一个单独的文件夹中)
Template.add_review.events({
'submit .add-review': function(event){
var rating = event.target.rating.value;
var review = event.target.review.value;
var recipeId = this._id;
var params = {
rating: rating,
review: review,
_id: recipeId
}
Meteor.call('addReview', params);
FlashMessages.sendSuccess('Review Added'); // and any other options you want to include
Router.go('reviews',{_id:recipeId});
return false;
}
});
&#13;
/lib/methods.js
Meteor.methods({
'addReview': function(params){
Reviews.insert(params);
}
});
&#13;
尝试一下,看看它是否有效。另外,将您的methods.js文件放在/ lib文件夹中,如Stephen建议的那样。