Simple question about template


#1

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


#2

You try

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

#3

thanks for the answer but problem is this line

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

if i remove this everything works fine.


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

what ex?


#5

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


#6

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


#7

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 ?


#8

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:


#9

Thank you mikewink … nice tip! :wink: