Antoine
Antoine

Reputation: 569

Method error using jquery

I keep getting that message in my console when calling my method:

Exception while invoking method 'displayAndAddPost' ReferenceError: $ is not defined

However everything works fine so I am not sure how to properly use Jquery and stop that error message.

Here is the invoked method:

Meteor.methods({

    displayAndAddPost: function(word, counter) {

    var exist = Posts.findOne({word: word})

        if (word == "") {
            console.log("empty")

        }else if(exist != undefined ){
            //console.log(exist._id)
            Posts.update(exist._id, {$inc: {counter: 1}});
            $(".posts_form").hide();
            $(".posts_index").show();   

        }else{
            Posts.insert({
                word: word,
                counter: counter,
                createdAt: new Date()
            });
            $(".posts_form").hide();
            $(".posts_index").show();           
        }       
    }


});

Please note that although it says the $ is not defined the Jquery actions still works.

Thanks for you help.

Upvotes: 1

Views: 25

Answers (1)

saimeunt
saimeunt

Reputation: 22696

Meteor methods code is executed both on the client and the server, however on the server jQuery is undefined because it's a browser only library.

You can enclose your jQuery browser specific code inside this.isSimulation sections to run them only on the client.

if(this.isSimulation){
  $(".posts_form").hide();
  $(".posts_index").show();
}

Alternatively, you should probably move the jQuery logic out of the method and execute it only on the client based on the result of the method execution.

Upvotes: 1

Related Questions