{"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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/base/connection.js","lineNumber":789,"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":771,"sourceCodeEnd":807,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/base/connection.js#L771-L807","documentation":"Connection.execute() iterates every element of the bind-values array and rejects `undefined`. SQL NULL must be expressed as the JS value `null`, not `undefined`, because `undefined` has no SQL representation and almost always indicates a missing or uninitialised value in application code. Catching this early prevents silent data corruption.","triggerScenarios":"Passing a values array containing an `undefined` element: `execute(sql, [id, maybeUndefined, name])` where a variable was never assigned. Common with optional request parameters, sparse arrays, destructuring with missing keys, or `JSON.parse` producing objects whose `Object.values()` include undefined.","commonSituations":"An optional API field that was not provided (so the variable is undefined); array built via `[a, b, c]` where one variable shadowing was wrong; spreading an object's values where a property is absent; a bug in parameter assembly.","solutions":["Coalesce undefined to null explicitly: `execute(sql, [id, value ?? null])`.","Find and fix the source of the undefined value in your parameter-building code.","Use a helper that maps undefined → null across the whole array before passing it."],"exampleFix":"// before\nconnection.execute('INSERT INTO t(a,b) VALUES (?,?)', [a, req.body.b]);\n\n// after\nconnection.execute('INSERT INTO t(a,b) VALUES (?,?)', [a, req.body.b ?? null]);","handlingStrategy":"validation","validationCode":"function sanitizeBinds(values) {\n  return values.map((v) => (v === undefined ? null : v));\n}\nconn.execute(sql, sanitizeBinds(rawValues));","typeGuard":"function hasNoUndefined(arr) {\n  return Array.isArray(arr) && arr.every((v) => v !== undefined);\n}","tryCatchPattern":null,"preventionTips":["Always coerce optional values with `?? null` before binding.","Add a unit test that asserts bind arrays contain no undefined.","Build bind arrays explicitly rather than via sparse spreads."],"tags":["query","bind-parameters","prepared-statements"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}