{"id":"bcb741c0163f1cff","repo":"sidorares/node-mysql2","slug":"user-connection-config-property-must-be-a-string","errorCode":null,"errorMessage":"\"user\" connection config property must be a string","messagePattern":"\"user\" connection config property must be a string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/packets/change_user.js","lineNumber":86,"sourceCode":"          connectAttributes[attrNames[k]],\n          encoding\n        );\n      }\n      packet.writeLengthCodedNumber(keysLength);\n      for (let k = 0; k < attrNames.length; ++k) {\n        packet.writeLengthCodedString(attrNames[k], encoding);\n        packet.writeLengthCodedString(\n          connectAttributes[attrNames[k]],\n          encoding\n        );\n      }\n    }\n    return packet;\n  }\n\n  toPacket() {\n    if (typeof this.user !== 'string') {\n      throw new Error('\"user\" connection config property must be a string');\n    }\n    if (typeof this.database !== 'string') {\n      throw new Error('\"database\" connection config property must be a string');\n    }\n    // dry run: calculate resulting packet length\n    const p = this.serializeToBuffer(Packet.MockBuffer());\n    return this.serializeToBuffer(Buffer.allocUnsafe(p.offset));\n  }\n}\n\nmodule.exports = ChangeUser;\n","sourceCodeStart":68,"sourceCodeEnd":98,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/packets/change_user.js#L68-L98","documentation":"When serialising a COM_CHANGE_USER packet, mysql2 requires the `user` property to be a string. If `changeUser()` is called with a config object whose `user` is missing or non-string (number, null, object), the packet cannot be built and the call fails fast with a clear error before any bytes hit the wire.","triggerScenarios":"Calling `connection.changeUser({ user: undefined })`, `changeUser({ user: 123 })`, or forgetting to include `user` in the options object. Also when passing a config built from unvalidated input.","commonSituations":"Pool-connection reuse where the new user comes from an untrusted/optional source and was not coerced to a string; a refactor that renamed a variable; passing the whole request body whose `user` field is absent.","solutions":["Always pass a string `user`: `connection.changeUser({ user: String(newUser), database })`.","Validate/coerce the user value before calling changeUser.","Default to a sensible string when the source value is missing."],"exampleFix":"// before\nconnection.changeUser({ user: req.body.userId });\n\n// after\nconnection.changeUser({ user: String(req.body.userId) });","handlingStrategy":"validation","validationCode":"function assertChangeUserOptions(opts) {\n  if (typeof opts.user !== 'string') throw new TypeError('changeUser: user must be a string');\n  if (typeof opts.database !== 'string') throw new TypeError('changeUser: database must be a string');\n}","typeGuard":"function isValidChangeUserOpts(opts) {\n  return opts != null && typeof opts.user === 'string' && typeof opts.database === 'string';\n}","tryCatchPattern":null,"preventionTips":["Always pass both user and database as strings to changeUser.","Coerce external inputs with String() before passing.","Unit-test changeUser calls with a fixture config."],"tags":["change-user","connection-config"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}