简体   繁体   中英

I get an error **Error invoking Method 'addReview': Internal server error [500]

If I try to add reviews or like a recipe I get an error Error invoking Method 'addReview': Internal server error [500] debug.js:41 ,even though it adds reviews and likes into databases and works fine but still gives me above error.

Source code Github

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;
            Meteor.call('addReview',rating,review,recipeId);
        }
    });
Template.recipes.events({
    "click [data-action='addLikes']": function (event) {
        event.preventDefault();
        var recipe = Recipes.findOne({_id: this._id});
        Meteor.call('upvote',recipe)
    }
});

client/methods.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}});
        }
    }
})

server/permissions.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;
    }
});

It seems that you are trying to use methods where they are not needed and only make things harder. Isn't this all just client code? If so, you can just use functions:

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

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

@Waqar First off, in your methods.js file, which as someone else has already told you should go in the /lib folder, you should have single quotes around your function name. I've re-written your code for Meteor.methods and for the Template.add-review.events below

client/templates/reviews/add_review.js (sorry, I put each template in a separate folder along with the js file for it)

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

/lib/methods.js

 Meteor.methods({ 'addReview': function(params){ Reviews.insert(params); } }); 

Give that a try and see if it works. Also, put your methods.js file in the /lib folder as Stephen suggested.

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