sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.07k stars 619 forks source link

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

Closed markddrake closed 1 month ago

markddrake commented 3 months ago

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
yadamu@1.0.0 C:\Development\YADAMU\src
+-- @aws-sdk/client-s3@3.507.0
+-- @aws-sdk/lib-storage@3.507.0
+-- @azure/storage-blob@12.17.0
+-- @electron/remote@2.1.2
+-- bootstrap-icons@1.11.3
+-- bootstrap@5.3.2
+-- cookie-parser@1.4.6
+-- csv-parser@3.0.0
+-- electron-packager@17.1.2
+-- electron@31.3.1
+-- express-session@1.18.0
+-- express@4.18.2
+-- font-awesome@4.7.0
+-- ibm_db_electron@npm:ibm_db@3.2.3
+-- ibm_db@3.2.3
+-- install@0.13.0
+-- jquery@3.7.1
+-- mariadb@3.3.0
+-- mime-types@2.1.35
+-- mongodb@6.3.0
+-- mssql@11.0.1
+-- mysql@2.18.1
+-- mysql2@3.11.0
+-- npm@10.4.0
+-- oracledb@6.3.0
+-- pg-copy-streams@6.0.6
+-- pg-query-stream@4.5.3
+-- pg@8.11.3
+-- readable-stream@4.5.2
+-- snowflake-sdk@1.9.3
+-- uuid@10.0.0
`-- wkx@0.5.0
markddrake commented 3 months ago

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

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

wellwelwel commented 1 month ago

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();