{"record":{"id":"0ce761b7718ee760","repo":"sidorares/node-mysql2","slug":"bind-parameters-must-not-contain-undefined-to-pas","errorCode":null,"errorMessage":"Bind parameters must not contain undefined. To pass SQL NULL specify JS null","messagePattern":"Bind parameters must not contain undefined\\. To pass SQL NULL specify JS null","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/base/connection.js","lineNumber":797,"sourceCode":"    }\n    this._resolveNamedPlaceholders(options);\n    // check for values containing undefined\n    if (options.values) {\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      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;","sourceCodeStart":779,"sourceCodeEnd":815,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/8b1f829d3706404ab372cf97bd77ebcf86578d97/lib/base/connection.js#L779-L815","documentation":"In Connection.execute (lib/base/connection.js:789-800), each bind parameter is checked and a value strictly equal to undefined throws, because mysql2 cannot encode undefined as a SQL value. The driver deliberately distinguishes undefined (a programming mistake) from null (which maps to SQL NULL). This is a fail-fast guard before the prepared-statement encode path.","triggerScenarios":"connection.execute(sql, [someVar]) where someVar was never assigned; destructuring an object that omits a key; a JSON.parse result missing an expected field; passing [undefined] explicitly.","commonSituations":"Optional request fields that are absent (req.body.middleName is undefined); a typo in a property name used to fill the params array; migration from a driver that silently coerced undefined to null.","solutions":["Coalesce to null explicitly: [value ?? null] to express SQL NULL for missing values.","Track down the source of the undefined and fix the data path so the value is intentional.","Add a pre-flight filter: values.map(v => v === undefined ? null : v) if a deliberate default-to-NULL policy is acceptable."],"exampleFix":"// before\nconnection.execute('INSERT INTO t (a, b) VALUES (?, ?)', [req.body.a, req.body.b]);\n\n// after\nconnection.execute('INSERT INTO t (a, b) VALUES (?, ?)', [req.body.a ?? null, req.body.b ?? null]);","handlingStrategy":"validation","validationCode":"const safeParams = (vals) => vals.map(v => v === undefined ? null : v);\n// conn.execute(sql, safeParams(values));","typeGuard":"const hasNoUndefined = (vals) => vals.every(v => v !== undefined);","tryCatchPattern":null,"preventionTips":["Coalesce optional values with ?? at the boundary where request data is read.","Add a unit test that feeds sparse/optional inputs to your query builder."],"tags":["prepared-statements","bind-parameters","api-usage","validation"],"backgroundTag":null,"analyzedSha":"8b1f829d3706404ab372cf97bd77ebcf86578d97","analyzedAt":"2026-08-11T02:54:28.964Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}