{"id":"68f7cd0c50a01d84","repo":"sidorares/node-mysql2","slug":"database-connection-config-property-must-be-a-st","errorCode":null,"errorMessage":"\"database\" connection config property must be a string","messagePattern":"\"database\" connection config property must be a string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/packets/change_user.js","lineNumber":89,"sourceCode":"      }\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":71,"sourceCodeEnd":98,"githubUrl":"https://github.com/sidorares/node-mysql2/blob/5ebe8903d6aea2d8ea1490e11b52491526e50f19/lib/packets/change_user.js#L71-L98","documentation":"When serialising a COM_CHANGE_USER packet, mysql2 requires the `database` property to be a string. A missing or non-string `database` (null, number, undefined) makes the packet unbuildable, so the call throws immediately. Note the property must be present as a string even if it is empty.","triggerScenarios":"Calling `connection.changeUser({ user: 'bob' })` without a `database` field; or `changeUser({ user, database: null })`. The constructor defaults `database` to `''`, but an explicit non-string overrides that and fails the check.","commonSituations":"Switching user but intending to keep/empty the current database and forgetting to pass `database: ''`; passing a numeric or object database value from unvalidated input.","solutions":["Always include `database` as a string, using `''` if you want no default database: `changeUser({ user: 'bob', database: '' })`.","Coerce the value: `changeUser({ user, database: String(dbName ?? '') })`."],"exampleFix":"// before\nconnection.changeUser({ user: 'bob' });\n\n// after\nconnection.changeUser({ user: 'bob', database: '' });","handlingStrategy":"validation","validationCode":"function changeUserSafe(conn, opts) {\n  const database = opts.database == null ? '' : String(opts.database);\n  return conn.changeUser({ ...opts, database: String(database) });\n}","typeGuard":"function isValidDatabase(db) {\n  return typeof db === 'string';\n}","tryCatchPattern":null,"preventionTips":["Always include `database` (use '' for none) in changeUser options.","Coerce numeric/null sources with String(x ?? '').","Validate pool-reuse configs before calling changeUser."],"tags":["change-user","connection-config"],"analyzedSha":"5ebe8903d6aea2d8ea1490e11b52491526e50f19","analyzedAt":"2026-08-03T18:58:53.602Z","schemaVersion":2}