Node-mysql2: Await connection.execute returns an object without data

Created on 19 Aug 2019  路  2Comments  路  Source: sidorares/node-mysql2

Code I am running:

async function main() {
    const mysql = require('mysql2');
    const connection = await mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'password',
        database: 'databaseName'
    });
    // query database
    const results = await connection.execute('SELECT * FROM uniqueItems WHERE mainKey = ?', [206]);
    console.log(results);
}
main();

Console output:

Execute {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  next: null,
  statement: undefined,
  sql: 'SELECT * FROM uniqueItems WHERE mainKey = ?',
  values: [ 206 ],
  onResult: undefined,
  parameters: [ 206 ],
  insertId: 0,
  _rows: [],
  _fields: [],
  _result: [],
  _fieldCount: 0,
  _rowParser: null,
  _executeOptions:
   { sql: 'SELECT * FROM uniqueItems WHERE mainKey = ?',
     values: [ 206 ] },
  _resultIndex: 0,
  _localStream: null,
  _unpipeStream: [Function],
  _streamFactory: undefined,
  _connection: null }

The query works in console correct:

MariaDB [databaseName]> SELECT * FROM uniqueItems WHERE mainKey = 206;
+---------+-----------+-------+--------------+-------------+
| mainKey | name      | grade | mainCategory | subCategory |
+---------+-----------+-------+--------------+-------------+
|     206 | SomeItem1 |     0 |           35 |           6 |
+---------+-----------+-------+--------------+-------------+

If I use: const [rows, fields] = await connection.execute() throws: TypeError: (intermediate value) is not iterable. Am I using it wrong or is it a bug?
mysql2: v1.6.5
node: v10.15.3

question

Most helpful comment

Am I using it wrong or is it a bug?

You are using original callback based api which returns Query object from execute()

change const mysql = require('mysql2'); to const mysql = require('mysql2/promise'); ( see https://github.com/sidorares/node-mysql2#using-promise-wrapper )

I'm surprised you didn't get error message, we tried to protect from this accidental usage with https://github.com/sidorares/node-mysql2/blob/caa9000888bd1dc3bce0e8fbaf8eae3af9b82d6b/lib/commands/query.js#L36-L42 ( see https://github.com/sidorares/node-mysql2/pull/813 )

All 2 comments

Am I using it wrong or is it a bug?

You are using original callback based api which returns Query object from execute()

change const mysql = require('mysql2'); to const mysql = require('mysql2/promise'); ( see https://github.com/sidorares/node-mysql2#using-promise-wrapper )

I'm surprised you didn't get error message, we tried to protect from this accidental usage with https://github.com/sidorares/node-mysql2/blob/caa9000888bd1dc3bce0e8fbaf8eae3af9b82d6b/lib/commands/query.js#L36-L42 ( see https://github.com/sidorares/node-mysql2/pull/813 )

@sidorares Should we investigate no error throwing? Coz im also not getting any errors, btw im using mysql2, without promise, but promises returned, is that expected?

for example
await db.promisePool.getConnection() - return promise
await conn.execute - return idk what, coz it's working sometimes(for example 2 queries on same connection, without any caching on mysql server) without await too, just returning nothing, empty array, that's strange.

conn.unprepare(query); return obj with/without await
conn.release() return undefined with/without await, so, i think that there is ways to detect wrong using style for devs.

So, can you explaing where we should use await and where it's sync.?

Was this page helpful?
0 / 5 - 0 ratings