Simple question about template

Hi

I have create this simple app to test login function.

This is the js code:

Template.Login.events({
‘submit #login’: function(event, template) {
event.preventDefault();
var username = template.find(’#login-username’).value,
password = template.find(’#login-password’).value;

       Meteor.loginWithPassword(username, password, function(error) {
        if (!error) {
            console.log('user');
        }
        else
        {
            template.find('#form-messages').html(error.reason);
            console.log(error.reason);
        }
        return;
    });

The link template.find create an error:

Exception in delivering result of invoking ‘login’:

I think it’s due to var “template” that can’t be invoke by loginWithPassword…

How i can fix it?

Thanks in advance

You try

var username = template.$('#login-username').val();
var password = template.$('#login-password').val();
if(username && password){
    Meteor.loginWithPassword....
}

thanks for the answer but problem is this line

template.find(’#form-messages’).html(error.reason);

if i remove this everything works fine.

try{
      template.find('#form-messages').html(error.reason);
}catch(ex){
      console.error(ex)
}

what ex?

message: “undefined is not a function (evaluating ‘template.find(’#form-messages’).html(error.reason)’)”

template.find(selector) return DOM element => template.find(’#form-message’).innerHTML = error.reasoan.
or
tempalte.$(selector) return Jquery object => template.$(’#form-messages’).html(error.reason);

Ok that’s work, now i undestand. I have find a tutorial with that code. Is any difference about performance to use jQuery inside Meteor ?

Hello Cesco,

why are you using the template to get the form values anyways? The values can be fetched from the event Object like so for example:

var username = event.target.username.value;
var password = event.target.password.value;

I hope that helps. :slight_smile:

Thank you mikewink … nice tip! :wink: