Currently, I am working on a project involving an express app and incorporating async/await in my routes. However, I've encountered an issue where it seems to be skipping the wait for the findOneUser call to complete before moving on to the next line:
app.post ('/api/authenticate', async(req, res) => {
console.log('GET TO AUTHENTICATE')
const { email, password } = req.body;
const user_db = await users.findOneUser( email );
console.log("user_db", user_db)
return user_db
});
const findOneUser = (email) => {
console.log("email", email)
pool.query('SELECT * FROM users where email = $1', [email], (error, results) => {
if (error) {
throw error
}
console.log("RESULT", results.rows)
results.rows
})
}
In the terminal logs below, you can observe that the user_db_log appears before the RESULT log. According to my understanding of async/await, the user_db code should have waited for the execution of the user.findOneUser method:
GET TO AUTHENTICATE
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d58455c504d51587d52484951525256135e5250">[email protected]</a>
user_db
undefined
RESULT
[
{
id: 11,
first_name: 'Test',
last_name: 'User',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01756472757472647341756472752f626e6c">[email protected]</a>'
}
]