CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 63 forks source link

`pool.getConnection()` not working when `returnArgumentsArray: true` is set in config #137

Closed Tawank closed 4 years ago

Tawank commented 4 years ago

If returnArgumentsArray is set in the config to true, node crashes on pool.getConnection() with an error:

Unhandled rejection TypeError: Cannot read property 'apply' of undefined
    at /path_to_project/node_modules/promise-mysql/lib/helper.js:26:45
    at Promise._execute (/path_to_project/node_modules/bluebird/js/release/debuggability.js:384:9)
    at Promise._resolveFromExecutor (/path_to_project/node_modules/bluebird/js/release/promise.js:518:18)
    at new Promise (/path_to_project/node_modules/bluebird/js/release/promise.js:103:10)
    at Array.promiseCallback (/path_to_project/node_modules/promise-mysql/lib/helper.js:8:16)
    at poolConnection.rollback (/path_to_project/node_modules/promise-mysql/lib/connection.js:71:32)
    at /path_to_project/helpers/mysql.js:43:24

My test app:

const config = require('./config.json');
const mysql = require('promise-mysql');

const createPool = mysql.createPool({
  host: config.DB_HOST,
  user: config.DB_USER,
  password: config.DB_PASSWORD,
  database: config.DB_NAME,
  returnArgumentsArray: true, // if is set to true the pool.getConnection() does not work
});

function testTransaction() {
  createPool.then(async (pool) => {
    const connection = await pool.getConnection();
    try {
      await connection.beginTransaction();
      const result = await connection.query('SELECT 1+1');
      console.log(result);
      await connection.commit();
      await connection.release();
    } catch (e) {
      await connection.rollback();
      await connection.release();
      throw e;
    }
  });
}
testTransaction();
CodeFoodPixels commented 4 years ago

Thanks for this, I've just published version 4.1.3 with a fix for this.