I'm currently using NodeJS v10.24.0, and having some trouble setting up a MySQL lookup to be asyncronous with ExpressJS.
Main:
app.get('/player/:username', async function (req, res) {
var uuid = await sqlMgr.lookupName(req.params.username, db, dbConf.prefix)
if (!uuid) {
res.status(404).send("Player not found");
} else {
res.redirect('/uuid/' + uuid);
}
})
sqlMgr.lookupName:
async function lookupName(username, db, prefix) {
const sql = "SELECT `uuid` FROM `" + prefix + "players` WHERE `playerName` = '" + username + "';";
db.query(sql, function (err, rows) {
if (err) {
throw (err);
}
let uuid = rows[0].uuid;
return uuid;
})
}
I've had a twiddle about, and slapped await and async everywhere possible, but the page will only render and uuid will return as undefined before the MySQL lookup finishes.