{"id":"a8d38585821be3e2","repo":"sidorares/node-mysql2","slug":"bind-parameters-must-not-contain-function-s-to-p","errorCode":null,"errorMessage":"Bind parameters must not contain function(s). To pass the body of a function as a string call .toString() first","messagePattern":"Bind parameters must not contain function\\(s\\)\\. To pass the body of a function as a string call \\.toString\\(\\) first","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/base/connection.js","lineNumber":794,"sourceCode":"      if (!Array.isArray(options.values)) {\n        throw new TypeError(\n          'Bind parameters must be array if namedPlaceholders parameter is not enabled'\n        );\n      }\n      options.values.forEach((val) => {\n        //If namedPlaceholder is not enabled and object is passed as bind parameters\n        if (!Array.isArray(options.values)) {\n          throw new TypeError(\n            'Bind parameters must be array if namedPlaceholders parameter is not enabled'\n          );\n        }\n        if (val === undefined) {\n          throw new TypeError(\n            'Bind parameters must not contain undefined. To pass SQL NULL specify JS null'\n          );\n        }\n        if (typeof val === 'function') {\n          throw new TypeError(\n            'Bind parameters must not contain function(s). To pass the body of a function as a string call .toString() first'\n          );\n        }\n      });\n    }\n    const executeCommand = new Commands.Execute(options, cb);\n\n    const prepareAndExecute = (errorCb) => {\n      const prepareCommand = new Commands.Prepare(options, (err, stmt) => {\n        if (err) {\n          // skip execute command if prepare failed\n          executeCommand.start = function () {\n            return null;\n          };\n          errorCb(err);\n          executeCommand.emit('end');\n          return;\n        }","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/base/connection.js#L776-L812","documentation":"Connection.execute() rejects bind values that are JS functions. A function cannot be serialised to a SQL value; this check exists because passing a callback or function reference where a value was expected is a common programming mistake (e.g. passing the wrong variable). The error message tells you to call `.toString()` explicitly if you genuinely intended to send the function's source as text.","triggerScenarios":"Passing a function reference into the values array: `execute(sql, [id, someCallback])`, or a variable that holds a function instead of data. Also happens when mapping over data and accidentally passing the mapper function.","commonSituations":"Mixing up a value and its accessor function; passing `req.body.someMethod` instead of `req.body.someField`; refactoring that left a function reference where a value used to be.","solutions":["Pass the value the function would return: `execute(sql, [id, getValue()])`.","If you truly want the function's source text: `execute(sql, [id, fn.toString()])`."],"exampleFix":"// before\nconnection.execute('SELECT * FROM t WHERE id = ?', [getId]);\n\n// after\nconnection.execute('SELECT * FROM t WHERE id = ?', [getId()]);","handlingStrategy":"validation","validationCode":"function assertNoFunctions(values) {\n  values.forEach((v, i) => {\n    if (typeof v === 'function') {\n      throw new TypeError(`Bind value at index ${i} is a function; did you mean to call it?`);\n    }\n  });\n}\nassertNoFunctions(values);\nconn.execute(sql, values);","typeGuard":"function hasNoFunctions(arr) {\n  return Array.isArray(arr) && arr.every((v) => typeof v !== 'function');\n}","tryCatchPattern":null,"preventionTips":["Double-check that each bind value is a scalar/Date/Buffer/object, not a callable.","Review refactors that replaced a value with its accessor function."],"tags":["query","bind-parameters","prepared-statements"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}