mongodb/node-mongodb-native · error · MongoParseError

Cannot have undefined values in key value pairs

Error message

Cannot have undefined values in key value pairs

What it means

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.

Solutions

  1. Ensure every comma-separated entry is 'key:value'.
  2. Remove stray commas or incomplete entries.

Example fix

// before
new MongoClient('mongodb://h/db?authMechanismProperties=SERVICE_NAME:mongodb,REPLY_REALM');
// after
new MongoClient('mongodb://h/db?authMechanismProperties=SERVICE_NAME:mongodb,REPLY_REALM:EXAMPLE.COM');
Defensive patterns

Strategy: validation

Validate before calling

function parseKeyValues(s: string): Array<[string, string]> {
  return s.split(',').filter(Boolean).map(pair => {
    const idx = pair.indexOf(':');
    if (idx === -1) throw new Error(`Missing ':' in '${pair}'`);
    const key = pair.slice(0, idx);
    const value = pair.slice(idx + 1);
    if (!key) throw new Error(`Empty key in '${pair}'`);
    return [key, value];
  });
}

Prevention

When it happens

Trigger: 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.

Common situations: Hand-editing authMechanismProperties for GSSAPI/Kerberos or OIDC and forgetting a colon, or malformed copy-paste.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/656ee627c2ff98e6. Report an issue: GitHub.

Appendix: source

Thrown at src/connection_string.ts:212

}

function getUIntFromOptions(name: string, value: unknown): number {
  const parsedValue = getIntFromOptions(name, value);
  if (parsedValue < 0) {
    throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);
  }
  return parsedValue;
}

function* entriesFromString(value: string): Generator<[string, string]> {
  if (value === '') {
    return;
  }
  const keyValuePairs = value.split(',');
  for (const keyValue of keyValuePairs) {
    const [key, value] = keyValue.split(/:(.*)/);
    if (value == null) {
      throw new MongoParseError('Cannot have undefined values in key value pairs');
    }

    yield [key, value];
  }
}

class CaseInsensitiveMap<Value = any> extends Map<string, Value> {
  constructor(entries: Array<[string, any]> = []) {
    super(entries.map(([k, v]) => [k.toLowerCase(), v]));
  }
  override has(k: string) {
    return super.has(k.toLowerCase());
  }
  override get(k: string) {
    return super.get(k.toLowerCase());
  }
  override set(k: string, v: any) {
    return super.set(k.toLowerCase(), v);

View on GitHub (pinned to dce7939f86)