I have the following function to login:
function login(username, password, callback) {
var myResponse;
var systemUser;
Users.getByLogin(username)
.then(function (response) {
systemUser = response.data;
});
// Linha testada
console.log("Senha: " + systemUser.password);
console.log("Senha informada: " + password);
if (typeof systemUser == "undefined") {
myResponse = { success: false, message: "Usuário inexistente!" };
} else if (password == systemUser.password) {
myResponse = { success: true, user: systemUser };
} else {
myResponse = { success: false, message: "Usuário ou senha incorretos!" };
}
callback(myResponse);
}
Normally the TypeError: Cannot read property 'password' of undefined
error occurs.
If I declare the variable systemUser
out of the function, looking at the comment line Linha testada
, the value gives undefined the first time I call the method, but the second time passes.