Oscar Dulzaides Oscar Dulzaides - 7 months ago 110
Javascript Question

Meteor method giving error

I am new to Meteor. I am trying to call a Meteor.method('addTask') from an event helper and I keep getting the error: "Error invoking Method 'addTask': Method 'addTask' not found [404]". I will put my code below:

Template.add_task.events({
'submit .js-emoticon': function(event){
event.preventDefault();
// console.log('clicked');
// var text = event.target.text.value;
// $('#text_display').html(text);
// $('#text_display').emoticonize();
Meteor.call("addTask");
}


});

And the Meteor.method here:

Meteor.methods({
'addTask':function(){
var task = event.target.text.value;
Items.insert({
created:new Date().toLocaleDateString("en-US"),
task:task
});
console.log(task);
}


});

Both are on the main.js in the client folder. I have tried to put the method on the server/main.js and I get an error: "Error invoking Method 'addTask': Internal server error [500]".

If it is on the client it will log the value of #text to console but on the server it doesn't even do that.

As I said I have been learning Meteor and researched this as the way to do it. I am obviously missing something and can't figure it out. Any help would be appreciated.

Answer

You are trying to look at a DOM element from your server code. You need to get that element on the client and then pass it to the method, which you can put in the /lib folder for latency compensation if you wish.

Client:

Template.add_task.events({
  'submit .js-emoticon': function(event){
    event.preventDefault();
    var task = event.target.text.value;
    Meteor.call("addTask",task);
  }
});

Server:

Meteor.methods({
  'addTask':function(task){
    check(task,String);
    Items.insert({ created: new Date(), task: task });
    console.log(task);
  }
});

You never want to convert your dates to strings when you persist them in mongo. That makes searching on dates impossible.