Search code examples
mysqlnode.jssequelize.jsnode-mysql

mysql pool.query doesn't work as expected


I have a router that sends mysql result as response like:

router

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
}

It'd been working really well but It just started to work weird randomly. What I expect from pool.query is obviously a result from sql query but what I get is query information like this.

Response

{
    "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
    }
}

I increased connectionLimit more but It didn't solve this problem. It's been working really well and I double checked the MYSQL account but there was nothing wrong with them. Any ideas why this is happening?


Solution

  • That's a query object, used in the library to represent a request to the database. If you call pool.query without a callback function, you get that back. Use node-mysql2 if you need a Promise-based API which will yield results when you await query calls.