mongodb/node-mongodb-native · error · MongoParseError
Cannot make read preference from ${JSON.stringify(value)}
Error message
Cannot make read preference from ${JSON.stringify(value)} What it means
When `readPreference` is an object with a `mode` key, the transform (src/connection_string.ts:1017-1023) calls `ReadPreference.fromOptions`; if that returns null (invalid mode or conflicting combination), it throws 'Cannot make read preference from ...'.
Source
Thrown at src/connection_string.ts:1023
});
}
},
readPreference: {
default: ReadPreference.primary,
transform({ values: [value], options }) {
if (value instanceof ReadPreference) {
return ReadPreference.fromOptions({
readPreference: { ...options.readPreference, ...value },
...value
});
}
if (isRecord(value, ['mode'] as const)) {
const rp = ReadPreference.fromOptions({
readPreference: { ...options.readPreference, ...value },
...value
});
if (rp) return rp;
else throw new MongoParseError(`Cannot make read preference from ${JSON.stringify(value)}`);
}
if (typeof value === 'string') {
const rpOpts = {
hedge: options.readPreference?.hedge,
maxStalenessSeconds: options.readPreference?.maxStalenessSeconds
};
return new ReadPreference(
value as ReadPreferenceMode,
options.readPreference?.tags,
rpOpts
);
}
throw new MongoParseError(`Unknown ReadPreference value: ${value}`);
}
},
readPreferenceTags: {
target: 'readPreference',
transform({View on GitHub (pinned to 3366c21a63)
Solutions
- Use a valid mode: 'primary','primaryPreferred','secondary','secondaryPreferred','nearest'
- Remove maxStalenessSeconds and tags when using 'primary'
- Pass readPreference as a string or a ReadPreference instance instead of an object
Example fix
// before
new MongoClient(uri, { readPreference: { mode: 'primary', maxStalenessSeconds: 5 } });
// after
new MongoClient(uri, { readPreference: { mode: 'nearest', maxStalenessSeconds: 5 } }); Defensive patterns
Strategy: validation
Validate before calling
const VALID_MODES = ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'];
function isValidReadPreferenceObject(v) {
if (!v || typeof v !== 'object') return false;
if (!VALID_MODES.includes(v.mode)) return false;
if (v.mode === 'primary' && (v.maxStalenessSeconds != null || (v.tags && v.tags.length))) return false;
return true;
}
if (options.readPreference && typeof options.readPreference === 'object' && !isValidReadPreferenceObject(options.readPreference)) {
throw new TypeError('Invalid readPreference object');
} Type guard
const VALID_MODES = new Set(['primary','primaryPreferred','secondary','secondaryPreferred','nearest']);
function isReadPreferenceMode(m) {
return VALID_MODES.has(m);
} Prevention
- Use a valid mode string
- Do not combine 'primary' with maxStalenessSeconds or tags
- Pass a ReadPreference instance or a plain string for clarity
When it happens
Trigger: `{ readPreference: { mode: 'fastest' } }` (invalid mode); `{ readPreference: { mode: 'primary', maxStalenessSeconds: 5 } }` (maxStaleness is invalid with primary); `{ readPreference: { mode: 'primary', tags: [{region:'eu'}] } }` (tags invalid with primary).
Common situations: Typos in mode; combining 'primary' with secondary-only options like tags or maxStalenessSeconds; stale config from a replica-set-only deployment.
Related errors
- Unknown ReadPreference value: ${value}
- Option "readPreference" must be a ReadPreference instance
- Descriptors missing a type must define a transform
- ${name} must be an object with 'username' and 'password' pro
- authMechanism one of ${mechanisms}, got ${value}
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/781ba53b0d64f84d.json.
Report an issue: GitHub.