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 · 2 comments · May be fixed by #3261
Open

PoolCluster : TypeError: cb is not a function #3091

jcmartineztiempo opened this issue Oct 2, 2024 · 2 comments · May be fixed by #3261
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

@jcmartineztiempo
Copy link
Author

jcmartineztiempo commented Dec 4, 2024

Adding some info:

I think that bug resides in PromisePoolCluster.of (promise.js). Instead of return a "PromisePoolNamespace" it returns another PromisePoolCluster:

of(pattern, selector) {
  return new PromisePoolCluster(
    this.poolCluster.of(pattern, selector),
    this.Promise,
  );
}

Maybe, creating a PromisePoolNamespace class like this, and returning an instance of this class by PromisePoolCluster.of, it will be fixed:

class PromisePoolNamespace {

  constructor(poolNamespace, thePromise) {
    this.poolNamespace = poolNamespace;
    this.Promise = thePromise || Promise;
  }

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

jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 4, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 4, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 5, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 6, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 10, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 10, 2024
jcmartineztiempo added a commit to jcmartineztiempo/node-mysql2 that referenced this issue Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants