{"record":{"id":"68c41be5c41e41c6","repo":"sidorares/node-mysql2","slug":"callback-function-is-not-available-with-promise-cl-68c41b","errorCode":null,"errorMessage":"Callback function is not available with promise clients.","messagePattern":"Callback function is not available with promise clients\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"promise.js","lineNumber":89,"sourceCode":"      corePoolCluster.getConnection(\n        pattern,\n        selector,\n        (err, coreConnection) => {\n          if (err) {\n            reject(err);\n          } else {\n            resolve(new PromisePoolConnection(coreConnection, this.Promise));\n          }\n        }\n      );\n    });\n  }\n\n  query(sql, args) {\n    const corePoolCluster = this.poolCluster;\n    const stackHolder = captureStackHolder(PromisePoolCluster.prototype.query);\n    if (typeof args === 'function') {\n      throw new Error(\n        'Callback function is not available with promise clients.'\n      );\n    }\n    return new this.Promise((resolve, reject) => {\n      const done = makeDoneCb(resolve, reject, stackHolder);\n      corePoolCluster.query(sql, args, done);\n    });\n  }\n\n  execute(sql, args) {\n    const corePoolCluster = this.poolCluster;\n    const stackHolder = captureStackHolder(\n      PromisePoolCluster.prototype.execute\n    );\n    if (typeof args === 'function') {\n      throw new Error(\n        'Callback function is not available with promise clients.'\n      );","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/8b1f829d3706404ab372cf97bd77ebcf86578d97/promise.js#L71-L107","documentation":"PromisePoolCluster.query() (the cluster-level query from 'mysql2/promise') throws synchronously when args is a function. The cluster promise wrapper routes queries across nodes and returns a promise; callbacks are not supported. Note this is the cluster itself, not a namespace obtained via .of().","triggerScenarios":"const cluster = createPoolCluster({}); cluster.add('node1', {...}); cluster.query('SELECT 1', (err, rows) => {...}) using the promise cluster.","commonSituations":"Direct cluster queries (routed across nodes) ported from callback examples; helpers that accept a cluster and a callback; confusion between cluster.query and namespace.query.","solutions":["Await the result: const [rows] = await cluster.query(sql).","Use the callback PoolCluster API (require('mysql2').createPoolCluster) for callback patterns.","Wrap the call in try/catch to handle the synchronous throw."],"exampleFix":"// before\ncluster.query('SELECT 1', (err, rows) => { /* ... */ });\n// after\nconst [rows] = await cluster.query('SELECT 1');","handlingStrategy":"type-guard","validationCode":"if (typeof args === 'function') {\n  throw new TypeError('cluster.query() in the promise API does not accept a callback; use await');\n}","typeGuard":"function isPromisePoolCluster(c) {\n  return c != null && 'Promise' in c && 'poolCluster' in c;\n}","tryCatchPattern":"try {\n  const [rows] = await cluster.query(sql, args);\n} catch (err) {\n  if (/Callback function is not available/.test(err.message)) {\n    // drop the callback and use await\n  } else {\n    throw err;\n  }\n}","preventionTips":["Treat cluster.query like any promise method — no callbacks.","Distinguish cluster.query (routed) from namespace.query; both are promise-only in mysql2/promise.","Migrate cluster helpers fully rather than partially."],"tags":["promise-api","callback","api-misuse","pool-cluster","migration"],"backgroundTag":null,"analyzedSha":"8b1f829d3706404ab372cf97bd77ebcf86578d97","analyzedAt":"2026-08-11T02:54:28.964Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}