{"record":{"id":"656ee627c2ff98e6","repo":"mongodb/node-mongodb-native","slug":"cannot-have-undefined-values-in-key-value-pairs","errorCode":null,"errorMessage":"Cannot have undefined values in key value pairs","messagePattern":"Cannot have undefined values in key value pairs","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":212,"sourceCode":"}\n\nfunction getUIntFromOptions(name: string, value: unknown): number {\n  const parsedValue = getIntFromOptions(name, value);\n  if (parsedValue < 0) {\n    throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);\n  }\n  return parsedValue;\n}\n\nfunction* entriesFromString(value: string): Generator<[string, string]> {\n  if (value === '') {\n    return;\n  }\n  const keyValuePairs = value.split(',');\n  for (const keyValue of keyValuePairs) {\n    const [key, value] = keyValue.split(/:(.*)/);\n    if (value == null) {\n      throw new MongoParseError('Cannot have undefined values in key value pairs');\n    }\n\n    yield [key, value];\n  }\n}\n\nclass CaseInsensitiveMap<Value = any> extends Map<string, Value> {\n  constructor(entries: Array<[string, any]> = []) {\n    super(entries.map(([k, v]) => [k.toLowerCase(), v]));\n  }\n  override has(k: string) {\n    return super.has(k.toLowerCase());\n  }\n  override get(k: string) {\n    return super.get(k.toLowerCase());\n  }\n  override set(k: string, v: any) {\n    return super.set(k.toLowerCase(), v);","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L194-L230","documentation":"Used when parsing comma-separated key:value pairs (notably authMechanismProperties and similar) via entriesFromString. Each pair must split into a key and a non-undefined value; a segment with no ':' separator yields value === undefined and is rejected.","triggerScenarios":"An authMechanismProperties (or comparable list) value like 'CANONICALIZE_HOST_NAME: true, SERVICE_NAME' where the second segment has no ':'. Also triggered by a trailing comma producing an empty segment that is not the empty-string short-circuit.","commonSituations":"Hand-editing authMechanismProperties for GSSAPI/Kerberos or OIDC and forgetting a colon, or malformed copy-paste.","solutions":["Ensure every comma-separated entry is 'key:value'.","Remove stray commas or incomplete entries."],"exampleFix":"// before\nnew MongoClient('mongodb://h/db?authMechanismProperties=SERVICE_NAME:mongodb,REPLY_REALM');\n// after\nnew MongoClient('mongodb://h/db?authMechanismProperties=SERVICE_NAME:mongodb,REPLY_REALM:EXAMPLE.COM');","handlingStrategy":"validation","validationCode":"function parseKeyValues(s: string): Array<[string, string]> {\n  return s.split(',').filter(Boolean).map(pair => {\n    const idx = pair.indexOf(':');\n    if (idx === -1) throw new Error(`Missing ':' in '${pair}'`);\n    const key = pair.slice(0, idx);\n    const value = pair.slice(idx + 1);\n    if (!key) throw new Error(`Empty key in '${pair}'`);\n    return [key, value];\n  });\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Author authMechanismProperties as an object in the options, not a string.","Validate properties strings at config-load time."],"tags":["connection-string","validation","auth","parsing"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}