node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Probably -VE Testcase: TypeError: cb is not a function at C:\Development\YADAMU\src\node_modules\mysql2\lib\pool.js:64:18

Open markddrake opened this issue 1 year ago • 1 comments

Given the following code


import mysql from 'mysql2'
import fs from 'fs'

class Test { 

	vendorProperties = {
	  "multipleStatements":true
	 ,"typeCast":true
	 ,"supportBigNumbers":true
	 ,"bigNumberStrings":true
	 ,"dateStrings":true
	 ,"trace":true
	 ,"user":"root"
	 ,"password": "oracle"
	 ,"host":"yadamu-db1"
	 ,"database":"sys"
	 ,"port":3306
	 , infileStreamFactory : (path) => {fs.createReadStream(path)}
	 }  
	 
  async createConnectionPool() {
    
    let stack, operation
	
    try {
      stack = new Error().stack;
      operation = 'mysql.createPool()'  
      this.pool = mysql.createPool(this.vendorProperties)
    } catch (e) {
      throw e
    }
    
  }

  async getConnectionFromPool() {

    let stack
    
    try {    
      stack = new Error().stack;
      const connection = await this.pool.getConnection()
      return connection
	} catch (err) {
	  throw err 
    }
  }
  
  async test() {
	  let results
	  try {
        await this.createConnectionPool()
	    const conn = await this.getConnectionFromPool()
	  } catch (e) {
		console.log(e)
	  }
  }

}

const test = new Test();
test.test().then(() => console.log('Success')).catch((e) => console.log(e))

I get

 C:\Development\YADAMU\src\node_modules\mysql2\lib\pool.js:64
          return cb(err);
                 ^

TypeError: cb is not a function
    at C:\Development\YADAMU\src\node_modules\mysql2\lib\pool.js:64:18
    at PoolConnection.<anonymous> (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:816:13)
    at Object.onceWrapper (node:events:629:26)
    at PoolConnection.emit (node:events:526:35)
    at PoolConnection._notifyError (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:252:12)
    at PoolConnection._handleFatalError (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:183:10)
    at PoolConnection.handlePacket (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:491:12)
    at PacketParser.onPacket (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:97:12)
    at PacketParser.executeStart (C:\Development\YADAMU\src\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.<anonymous> (C:\Development\YADAMU\src\node_modules\mysql2\lib\connection.js:104:25)

Node.js v20.7.0
C:\Development\YADAMU>node -v
v20.7.0
C:\Development\YADAMU>cd src

C:\Development\YADAMU\src>npm ls
[email protected] C:\Development\YADAMU\src
+-- @aws-sdk/[email protected]
+-- @aws-sdk/[email protected]
+-- @azure/[email protected]
+-- @electron/[email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- ibm_db_electron@npm:[email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]

markddrake avatar Aug 04 '24 18:08 markddrake

FYI: Cause by not importing 'mysql2/promise'.

However, I would not expect an internal error to bubble up...

markddrake avatar Aug 04 '24 19:08 markddrake

Hi, @markddrake. Sorry, I missed this issue 🙋🏻‍♂️

It happens due to:

import mysql from 'mysql2'

// ...

this.pool = mysql.createPool(this.vendorProperties)

// ...

// ❌ this method expects for a callback (function), but gets an undefined
const connection = await this.pool.getConnection()
return connection

By using mysql import, you need to use a callback to get the connection, for example:

let connection;

this.pool.getConnection((err, conn) => {
  connection = conn;
});

return connection;

As an alternative, you also can use it like:

this.pool = mysql.createPool(this.vendorProperties).promise();

// Now, it will work
const connection = await this.pool.getConnection()

However, I would not expect an internal error to bubble up...

It's like trying to execute an undefined:

const cb = undefined;

// ❌ TypeError: cb is not a function
cb();

wellwelwel avatar Sep 09 '24 11:09 wellwelwel