Home nodejs await not working
Reply: 0

nodejs await not working

user2281
1#
user2281 Published in April 19, 2018, 9:56 am

I am tying to use await to get my function to execute one line at a time. So in this case, call checkSession to get the username, display it and then sent it back in respond.

However, it is displaying the username and returning it in respond before checkSession finish executing. How am I using await wrong in this case?

app.get('/getUsernameService', async function(req, res) 
{
    console.log("getUsernameService");
    var username = await checkSession(req.cookies.SESSION);
    console.log("name:"+username);
    res.end(username);
})

async function checkSession(sessionID)
{
    console.log("checkSession");
    var validSession = "no";
    oracledb.getConnection(
        DBconfig,
        function(err, connection)
        {
            if (err) { console.error(err); return; }
            connection.execute(
                "Select count(username) From session where id = '"+sessionID+"'",
                function(err, result)
                {
                    if (err) { console.error(err); doRelease(connection); return; }
                    response =  result.rows;  
                    console.log("result"+result.rows);
                    var resultCount = result.rows[0][0];
                    console.log("result2["+resultCount+"]"+(resultCount>0));
                    if(resultCount>0){
                        validSession = "yes";
                    }
                    if(validSession=="yes"){
                        connection.execute(
                            "Select username From session where id = '"+sessionID+"'",
                            function(err, result)
                            {
                                if (err) { console.error(err); doRelease(connection); return; }
                                response =  result.rows;  
                                console.log("what is username"+response);
                                return response;
                            }
                        );   
                    }else{
                        console.log("no name");
                        return "invalid";
                    }               
                }
            );
            doRelease(connection);  
        }
    );

}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300238 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO