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
Thrown by entriesFromString() when parsing a comma-separated key:value option string and a segment lacks the value after the colon. Used for options parsed as maps such as readPreferenceTags and authMechanismProperties. Splitting 'key:value' on the first ':' yields an undefined value when the segment is 'key' with no colon or ends with a colon. It is a MongoParseError.
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 3366c21a63)
Solutions
- Ensure every tag/mproperty segment is 'key:value' with both parts non-empty.
- Filter empty segments before joining: tags.filter(t => t).join(',').
- Validate the built string with a regex like /^[^,]+:[^,]+(,[^,]+:[^,]+)*$/ before passing.
- Avoid trailing/leading commas and trailing colons in templated values.
Example fix
// before
const tags = [`region:${r}`, zone ? `zone:${zone}` : ''].filter(Boolean).join(',');
// trailing segment 'zone:' if zone present without value
// after
const tags = [{ region: r }, zone ? { zone } : null]
.filter(Boolean)
.map(Object.entries)
.map(([k, v]) => `${k}:${v}`)
.join(',');
new MongoClient(uri, { readPreferenceTags: tags ? [tags] : undefined }); Defensive patterns
Strategy: validation
Validate before calling
function buildTagString(entries: Record<string, string>[]): string {
const valid = entries
.flatMap(Object.entries)
.filter(([k, v]) => k && v)
.map(([k, v]) => `${k}:${v}`);
if (!valid.length) return '';
if (!/^[^,]+:[^,]+(,[^,]+:[^,]+)*$/.test(valid.join(','))) {
throw new Error('Malformed key:value tag string');
}
return valid.join(',');
} Type guard
const isKeyValueSegments = (s: string): boolean =>
s.split(',').every(seg => /^[^:]+:[^:]+$/.test(seg)); Try / catch
try {
await client.connect();
} catch (e) {
if (e instanceof MongoParseError && /undefined values in key value pairs/.test(e.message)) {
throw new Error('A readPreferenceTags/authMechanismProperties segment is missing a value');
}
throw e;
} Prevention
- Filter empty segments before joining tag strings.
- Validate the regex /^[^,]+:[^,]+(,[^,]+:[^,]+)*$/ before passing.
- Avoid trailing commas and trailing colons when templating.
When it happens
Trigger: URI like ?readPreferenceTags=region:us-east,zone or ?authMechanismProperties=AWS_SESSION_TOKEN: (trailing colon); a tag without a value: ?readPreferenceTags=,; malformed comma-separated entries with stray commas.
Common situations: Building readPreferenceTags dynamically and emitting a trailing comma; templating authMechanismProperties with an unset env var producing 'KEY:'; copy-paste from docs that showed a placeholder; concatenating tags with a leading/trailing comma.
Related errors
- ${name} must be either "true" or "false"
- Expected ${name} to be stringified int value, got: ${value}
- ${name} can only be a positive int value, got: ${value}
- URI option "${key}" cannot appear more than once in the conn
- URI option "${key}" cannot be specified with no value
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/656ee627c2ff98e6.json.
Report an issue: GitHub.