0

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.

Permanently
  • 344
  • 2
  • 3
  • 13
  • To add - I've checked the variables, and the MySQL statement and lookup is not the problem as it returns the result I want. However, It's just simply the page rendering a 404 page with the variable being undefined before the SQL statement completes. – Permanently Mar 02 '21 at 20:28
  • 1
    FYI, see the 10 points of advice [here](https://stackoverflow.com/questions/66191127/promise-me-return-a-pending-state-when-i-use-a-callback/66191335#66191335) for working with promises and `await`. – jfriend00 Mar 02 '21 at 23:29

0 Answers0