question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue promisifying node-mysql

See original GitHub issue

I would expect the following two code fragments to output the same thing:

Promise = require 'bluebird'
mysql = require 'mysql'
Promise.promisifyAll(require("mysql/lib/Pool").prototype)
Promise.promisifyAll(require("mysql/lib/Connection").prototype)

pool = mysql.createPool { database: 'mydb', host: 'localhost', user: 'user', password: 'pw' }

pool.getConnectionAsync().then (db) ->
  db.queryAsync('select * from users where id=?', '1').then (rows) ->
    console.log rows
mysql = require 'mysql'

pool = mysql.createPool { database: 'mydb', host: 'localhost', user: 'user', password: 'pw' }

pool.getConnection (err, db) ->
  db.query('select * from users where id='?', '1', (err, rows) ->
    console.log rows

However for the first, the output looks like:

[ [ { id: 1, email: 'myemail', password: 'pw' } ],
      [ { catalog: 'def',
  db: 'mydb',
  table: 'users',
  orgTable: 'users',
  name: 'id',
  orgName: 'id',
  charsetNr: 63,
  length: 64,
  type: 8,
  flags: 20483,
  decimals: 0,
  default: undefined,
  zeroFill: false,
  protocol41: true }, ...
] ]

and for the second the output is just:

 [ { id: 1, email: 'myemail', password: 'pw' } ] 

I’m still digging in, but wanted to post an issue to see if this is a known problem (or if it’s a case of PEBKAC)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
derdrdirkcommented, Jan 20, 2018

@Prieston no. You need to release it yourself. Have a look at Bluebirds .disposer()

1reaction
petkaantonovcommented, Jan 14, 2015

Mysql doesn’t strictly conform to node callback convention, the callback parameters are actually: is err, rows, columns which doesn’t map to single return value. So the promise fulfillment value is [rows, columns] instead of just rows like you expected, to avoid information loss.

You can use spread instead:

pool.getConnectionAsync().then (db) ->
  db.queryAsync('select * from users where id=?', '1').spread (rows, columns) ->
    console.log rows

This cannot be fixed until 3.0.0, see https://github.com/petkaantonov/bluebird/issues/307

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql-promisify - npm
Start using mysql-promisify in your project by running `npm i ... There are no other projects in the npm registry using mysql-promisify.
Read more >
How to promisify a mysql pool connection in Node js?
Here is an easy example about how I solved it: pool.getConnection = util.promisify(pool.getConnection) let conn = await pool.
Read more >
Promisifying MySQL Transactions with Connection Pool
Promisifying MySQL Transactions with Connection Pool. This is a Node.js example on MySQL transactions with promises (using async / await).
Read more >
Node.js, MySQL and promises - codeburst
Like most I/O operations in Node.js, database access is asynchronous. ... First we have to “promisify” the database client.
Read more >
Promisification - The Modern JavaScript Tutorial
But promises are more convenient, so it makes sense to promisify them. ... In Node.js, there's a built-in util.promisify function for that.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found