{"id":"377c7aba5e461e6c","repo":"sidorares/node-mysql2","slug":"callback-function-is-not-available-with-promise-cl-377c7a","errorCode":null,"errorMessage":"Callback function is not available with promise clients.","messagePattern":"Callback function is not available with promise clients\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/promise/pool.js","lineNumber":42,"sourceCode":"      corePool.getConnection((err, coreConnection) => {\n        if (err) {\n          reject(err);\n        } else {\n          resolve(new PromisePoolConnection(coreConnection, this.Promise));\n        }\n      });\n    });\n  }\n\n  releaseConnection(connection) {\n    if (connection instanceof PromisePoolConnection) connection.release();\n  }\n\n  query(sql, args) {\n    const corePool = this.pool;\n    const stackHolder = captureStackHolder(PromisePool.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      if (args !== undefined) {\n        corePool.query(sql, args, done);\n      } else {\n        corePool.query(sql, done);\n      }\n    });\n  }\n\n  execute(sql, args) {\n    const corePool = this.pool;\n    const stackHolder = captureStackHolder(PromisePool.prototype.execute);\n    if (typeof args === 'function') {\n      throw new Error(","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/promise/pool.js#L24-L60","documentation":"PromisePool.query() validates its second argument at pool.js:41 and throws synchronously if it is a function. A pool obtained via createPool(...).promise() or require('mysql2/promise').createPool() exposes only the promise-based query; a node-style callback is not accepted.","triggerScenarios":"Calling pool.promise().query('SELECT 1', cb), or pool.query('SELECT 1', cb) on a pool from mysql2/promise. Also when refactoring callback code and leaving the callback in the args position.","commonSituations":"Mixing require('mysql2') (callback) with require('mysql2/promise') (promise) in the same codebase, or copy-pasting callback examples from docs into a promise-based project.","solutions":["Await the promise: const [rows] = await pool.query('SELECT 1')","Pass params as an array: await pool.query('SELECT ? FROM t', [col])","If you truly need callbacks, import from 'mysql2' (not 'mysql2/promise') and skip .promise()"],"exampleFix":"// before\nconst mysql = require('mysql2/promise');\nconst pool = mysql.createPool({});\npool.query('SELECT 1', (err, rows) => {}); // throws\n\n// after\nconst [rows] = await pool.query('SELECT 1');","handlingStrategy":"type-guard","validationCode":"function promisePoolQuery(pool, sql, params) {\n  if (typeof params === 'function') {\n    throw new TypeError('mysql2 promise pool: do not pass a callback to query(); await the result');\n  }\n  return params !== undefined ? pool.query(sql, params) : pool.query(sql);\n}","typeGuard":"function isCallbackArg(arg) {\n  return typeof arg === 'function';\n}","tryCatchPattern":"try {\n  const [rows] = await pool.query('SELECT 1');\n} catch (err) {\n  // synchronous throw: wrap must cover the call itself\n  console.error(err);\n}","preventionTips":["Pick one API style project-wide: either require('mysql2') with callbacks or require('mysql2/promise')","When refactoring, grep for '.query(' and remove any inline arrow callbacks on promise pools","Type your pool as mysql2/promise's Pool so the TS compiler flags callback misuse"],"tags":["promise-api","callback","api-misuse","pool"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}