Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoolCluster : TypeError: cb is not a function #3091

Open
jcmartineztiempo opened this issue Oct 2, 2024 · 1 comment
Open

PoolCluster : TypeError: cb is not a function #3091

jcmartineztiempo opened this issue Oct 2, 2024 · 1 comment
Labels

Comments

@jcmartineztiempo
Copy link

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
@wellwelwel wellwelwel added the bug label Oct 2, 2024
@wellwelwel
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants