Skip to content

PoolCluster : TypeError: cb is not a function #3091

Closed
@jcmartineztiempo

Description

@jcmartineztiempo

This error occurs when you call getConnection method on PoolNamespace instance.

The lib/pool_cluster.js file requires a cb parameter (1 argument) but it's undefined because PromisePoolCluster.getConnection in promise.js sends three (3) arguments: pattern, selector and a callback function, so wether calls getConnection without any argument, the cb argument is always undefined.

MyClass.ts

private get connection(): Promise<PoolConnection> {
  return this._connection ??= this.pool.getConnection();
}

promise.js

class PromisePoolCluster extends EventEmitter {
  constructor(poolCluster, thePromise) {
    super();
    this.poolCluster = poolCluster;
    this.Promise = thePromise || Promise;
    inheritEvents(poolCluster, this, ['warn', 'remove']);
  }

  getConnection(pattern, selector) {
    const corePoolCluster = this.poolCluster;
    return new this.Promise((resolve, reject) => {
      corePoolCluster.getConnection(pattern, selector, (err, coreConnection) => {
        if (err) {
          reject(err);
        } else {
          resolve(new PromisePoolConnection(coreConnection, this.Promise));
        }
      });
    });
  }

pool_cluster.js

class PoolNamespace {
  constructor(cluster, pattern, selector) {
    this._cluster = cluster;
    this._pattern = pattern;
    this._selector = makeSelector[selector]();
  }

  getConnection(cb) {
    const clusterNode = this._getClusterNode();
    if (clusterNode === null) {
      return cb(new Error('Pool does Not exists.'));
    }
    return this._cluster._getConnection(clusterNode, (err, connection) => {
      if (err) {
        return cb(err);
      }
      if (connection === 'retry') {
        return this.getConnection(cb);
      }
      return cb(null, connection);
    });
  }

Environment

  • nodejs: v20.11.1
  • mysql2: v3.11.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions