0
I have a route check login, it calls a method that returns a Boolean, I need to make an if with the value returned, but it returns Undefined, as it has not yet been executed.
app.post('/home', urlencodeParser, (req, res) => {
var resLogin = cruds.verificaLogin(req.body.username, req.body.password)
console.log(resLogin)
res.send('<h1>Olá Mundo</h1>')
})
Method verificaLogin
:
verificaLogin(_email, _senha) {
var res = new Boolean(0)
this.sql.query(`SELECT COUNT(*) login [login] FROM users where email = ${_email} and senha = ${_senha}`, function(erro, results, fields){
//if (!erro)
console.log(res)
return res
})
}
You need to use callbacks or adapt your code to use promises. It’s also important to understand how asynchrony works in Javascript.
– Luiz Felipe
var resLogin = cruds.verificaLogin(req.body.username, req.body.password)
here is using synchronously a method that inside has an asynchronous flame (this.sql.query(....
), That’s not gonna work– Ricardo Pontual