在继续使用 NodeJS 之前等待异步函数

问题描述

我目前使用的是 NodeJS v10.24.0,并且在将 MysqL 查找设置为与 Expressjs 异步时遇到了一些问题。

主要内容

    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,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;
    })
}

我一直在胡思乱想,并在任何可能的地方使用 await 和 async,但在 MysqL 查找完成之前,页面只会呈现并且 uuid 将返回为 undefined。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)