The problem is that when I make a POST call to /login
, the const user
gets assigned undefined even though getUser()
always returns the same value. The call console.log(getUser());
at the end of the file works fine.
I simplified and unified the code.
index.js
const express = require("express");
const secret = "123456789";
const jwt = require("jsonwebtoken");
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: '',
database: "test",
connectionLimit: 5
});
const app = express();
const port = process.env.PORT || 3000;
async function getUser() {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query("SELECT 'John' as username");
console.log(rows); // Prints [ { username: 'John' } ]
return rows;
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
}
app.post('/login', async (req, res) => {
const user = await getUser();
console.log(user); // Prints undefined
if (!user) {
return res.status(401).send({
success: false,
msg: "Usuario o contraseña incorrecta"
})
}
if (user) {
const payload = {
id: user.email,
dni: user.dni
};
const token = jwt.sign(payload, secret, { expiresIn: "1d" });
return res.status(200).send({
success: true,
token: 'Bearer ' + token
})
}
});
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
console.log(getUser());
package.json
{
"dependencies": {
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mariadb": "^3.1.1",
"nodemon": "^2.0.22",
"passport": "^0.6.0"
}
}
I played around with the async/await, looked through the documentation and searched similar problems but can’t find what is wrong.
2
Answers
The problem in your code is that you have a
return
statement in thefinally
block.When you have a return in your
finally
block, thereturn
s intry
orcatch
blocks do not get executed. So, your function returns the result ofreturn conn.end();
.Remove the
return
word from thefinally
block and the function should return what you expect.MDN finally-block documentation: