How to make the block wait the execution of a method to continue

Asked

Viewed 13 times

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.

  • 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

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.