According to the node-mysql documentation, the callback to a query will get three parameters - an error, the row(s) or result of the query, and something called fields
which may or may not be passed everytime.
fields will contain information about the returned results fields (if any)
I recently ran into a bug which resulted from node-mysql returning fields
in my production environment, but not in my staging environment. I was using async.auto
and passed the step callback directly to mysql.query
. In production, async.auto
combined rows
and fields
into an arguments
array, which lead to that part of the service breaking down because it got data in an unexpected format.
I could not get my staging environment to have fields
be passed to the callback, and when I tried locally I could not get mysql
to not pass fields
. Is there any way to know when fields
will be passed by mysql.query
and when will it not?
Essentially
async.auto({
runQuery: (next) => {
mysql.query(sql, values, next);
}
},
(err, results) => {
// if `fields` is not passed to the callback by mysql.query,
// then results.runQuery will be a row, or an array of rows -
// [
// [
// [
// {
// "columnValue": 1
// }
// ],
// [
// {
// "columnValue": 1
// }
// ]
// ]
//
// instead, if `fields` is passed to the callback by mysql.query,
// then results.queryQuery will be an array containing the row(s)
// and the fields
//
// [
// [
// [
// {
// "columnValue": 1
// }
// ],
// [
// {
// "columnValue": 1
// }
// ]
// ],
// [
// [
// {
// "catalog": "def",
// "db": "",
// "table": "",
// "orgTable": "",
// "name": "columnValue",
// "orgName": "",
// "charsetNr": 63,
// "length": 21,
// "type": 8,
// "flags": 129,
// "decimals": 0,
// "zeroFill": false,
// "protocol41": true
// }
// ],
// [
// {
// "catalog": "def",
// "db": "",
// "table": "",
// "orgTable": "",
// "name": "columnValue",
// "orgName": "",
// "charsetNr": 63,
// "length": 21,
// "type": 8,
// "flags": 129,
// "decimals": 0,
// "zeroFill": false,
// "protocol41": true
// }
// ]
// ]
// ]
});