Problem with asynchronous nodejs

Asked

Viewed 29 times

-1

I’m with a method in a class that calls the method another class and after that he gives a console.log of the return of this method. But this console.log is executed before the function call causing the result of the console.log be it undefined:

sign(data = {})
{
    
    let result = this.Verify.sign(data)

    if(result != 'ok')
    {
        return 'false'
    }
    else{
        //INSERIR PERSON E USER   
        
        let obj = {'desname': data['desname'], 'dtbirth': ''}

        this.Query.insert('tb_person', obj)   
        .then( (response) => {
            console.log(response)
        })
        .catch((error) => {
            console.log(error)
        })

    } 

}

async insert(table, obj){   

    let sql = `INSERT INTO ${table} SET ?`
    
    conect.query(sql, obj, (error, result) => {

        if(error)
        {
            return 'falid'
        }
        else
        {                  
            return 'ok'
        }
    }) 
}
  • 1

    That function Query.insert is returning a Promise? These three little dots underneath the await indicates that it has no effect on this code, it would be good to include the code of this function also in your question, and preferably as text, not as image.

  • Read the question I mentioned. Based on the code of insert that you shared, you need to return a file when the callback .query is completed.

  • I will edit my question just a moment

No answers

Browser other questions tagged

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