{"record":{"id":"729719249f24415f","repo":"mongodb/node-mongodb-native","slug":"name-must-be-an-object-with-username-and-pas","errorCode":null,"errorMessage":"${name} must be an object with 'username' and 'password' properties","messagePattern":"(.+?) must be an object with 'username' and 'password' properties","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":661,"sourceCode":"   * @param options - the options so far for resolution\n   * @param values - the possible values in precedence order\n   */\n  transform?: (args: { name: string; options: MongoOptions; values: unknown[] }) => unknown;\n}\n\nexport const OPTIONS = {\n  enableOverloadRetargeting: {\n    default: false,\n    type: 'boolean'\n  },\n  appName: {\n    type: 'string'\n  },\n  auth: {\n    target: 'credentials',\n    transform({ name, options, values: [value] }): MongoCredentials {\n      if (!isRecord(value, ['username', 'password'] as const)) {\n        throw new MongoParseError(\n          `${name} must be an object with 'username' and 'password' properties`\n        );\n      }\n      return MongoCredentials.merge(options.credentials, {\n        username: value.username,\n        password: value.password\n      });\n    }\n  },\n  authMechanism: {\n    target: 'credentials',\n    transform({ options, values: [value] }): MongoCredentials {\n      const mechanisms = Object.values(AuthMechanism);\n      const [mechanism] = mechanisms.filter(m => m.match(RegExp(String.raw`\\b${value}\\b`, 'i')));\n      if (!mechanism) {\n        throw new MongoParseError(`authMechanism one of ${mechanisms}, got ${value}`);\n      }\n      let source = options.credentials?.source;","sourceCodeStart":643,"sourceCodeEnd":679,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L643-L679","documentation":"Thrown by MongoParseError at src/connection_string.ts:661 in the `auth` option transform when the value passed as `auth` is not an object containing both `username` and `password` properties (checked via `isRecord(value, ['username','password'])`). `${name}` is `auth`.","triggerScenarios":"`new MongoClient(uri, { auth: 'bob' })`, `{ auth: { user: 'bob' } }` (wrong key), `{ auth: ['bob','pass'] }`, or omitting one of the two fields.","commonSituations":"Using `user` instead of `username` (a very common typo because other APIs use `user`); passing only username when password is also required by the option schema.","solutions":["Pass `auth` as `{ username: string, password: string }` — both keys required, spelled exactly `username` and `password`.","If you only need credentials, prefer the URI form `mongodb://username:password@host/` which fills the same fields."],"exampleFix":"// before\nnew MongoClient(uri, { auth: { user: 'bob', password: 'secret' } })\n\n// after\nnew MongoClient(uri, { auth: { username: 'bob', password: 'secret' } })","handlingStrategy":"type-guard","validationCode":"function isValidAuth(v) { return v != null && typeof v === 'object' && !Array.isArray(v) && 'username' in v && 'password' in v; }","typeGuard":"function isAuthObject(v): v is { username: string; password: string } { return !!v && typeof v === 'object' && !Array.isArray(v) && typeof v.username === 'string' && typeof v.password === 'string'; }","tryCatchPattern":"try { new MongoClient(uri, { auth }); } catch (e) { if (e instanceof MongoParseError && /must be an object with 'username' and 'password'/.test(e.message)) { auth = { username: auth.user ?? auth.username, password: auth.pass ?? auth.password }; } else throw e; }","preventionTips":["Use the exact keys `username` and `password` (not `user`/`pass`).","Prefer the URI form `mongodb://user:pass@host/` to avoid key-name typos."],"tags":["connection-string","authentication","options","type-validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}