mysql pool.query无法按预期工作

时间:2018-02-06 23:25:44

标签: mysql node.js mysqli sequelize.js node-mysql

我有一个路由器发送mysql结果作为响应,如:

路由器

var pool = mysql.createPool({
    connectionLimit: account.MYSQL_CONNECTION_LIMIT,
    host: account.MYSQL_HOST,
    database: account.MYSQL_DATABASE,
    port: account.MYSQL_PORT,
    user: account.MYSQL_USER_ID,
    password: account.MYSQL_PASSWORD
})

router.get('/', async function (req, res) => {
    res.json({
            'activities': await pool.query(actQ(uid,gid))
     })
})

function actQ (uid, gid) {
    var query = "SELECT date_time, steps, calories, floors, distance, duration, pace FROM ACTIVITY ";
    query += " WHERE user_id = " + uid;
    query += "   AND game_id = " + mysql.escape(gid);

    return query
}

它工作得非常好,但它开始随机工作。我对pool.query的期望显然是来自sql查询的结果,但我得到的是这样的查询信息。

响应

{
    "activities": {
        "domain": null,
        "_events": {},
        "_eventsCount": 0,
        "_callSite": {},
        "_ended": false,
        "_idleNext": null,
        "_idlePrev": null,
        "_idleStart": null,
        "_idleTimeout": -1,
        "_repeat": null,
        "sql": "SELECT date_time, steps, calories, floors, distance, duration, pace FROM ACTIVITY  WHERE user_id = 42741e32-b517-4655-92ca-af29815d236c   AND game_id = '498b36ff-0b90-11e8-97c8-061227ce9ef2'",
        "typeCast": true,
        "nestTables": false,
        "_resultSet": null,
        "_results": [],
        "_fields": [],
        "_index": 0,
        "_loadError": null
    }
}

我增加了connectionLimit,但它没有解决这个问题。它工作得非常好,我仔细检查了MYSQL帐户,但它们没有任何问题。任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

这是一个查询对象,在库中用于表示对数据库的请求。如果你在没有回调函数的情况下调用pool.query,那么你就可以了。如果您需要基于承诺的API,请使用node-mysql2,这会在您await查询电话时产生结果。