{"id":"781ba53b0d64f84d","repo":"mongodb/node-mongodb-native","slug":"cannot-make-read-preference-from-json-stringify","errorCode":null,"errorMessage":"Cannot make read preference from ${JSON.stringify(value)}","messagePattern":"Cannot make read preference from (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":1023,"sourceCode":"      });\n    }\n  },\n  readPreference: {\n    default: ReadPreference.primary,\n    transform({ values: [value], options }) {\n      if (value instanceof ReadPreference) {\n        return ReadPreference.fromOptions({\n          readPreference: { ...options.readPreference, ...value },\n          ...value\n        });\n      }\n      if (isRecord(value, ['mode'] as const)) {\n        const rp = ReadPreference.fromOptions({\n          readPreference: { ...options.readPreference, ...value },\n          ...value\n        });\n        if (rp) return rp;\n        else throw new MongoParseError(`Cannot make read preference from ${JSON.stringify(value)}`);\n      }\n      if (typeof value === 'string') {\n        const rpOpts = {\n          hedge: options.readPreference?.hedge,\n          maxStalenessSeconds: options.readPreference?.maxStalenessSeconds\n        };\n        return new ReadPreference(\n          value as ReadPreferenceMode,\n          options.readPreference?.tags,\n          rpOpts\n        );\n      }\n      throw new MongoParseError(`Unknown ReadPreference value: ${value}`);\n    }\n  },\n  readPreferenceTags: {\n    target: 'readPreference',\n    transform({","sourceCodeStart":1005,"sourceCodeEnd":1041,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L1005-L1041","documentation":"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 ...'.","triggerScenarios":"`{ 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).","commonSituations":"Typos in mode; combining 'primary' with secondary-only options like tags or maxStalenessSeconds; stale config from a replica-set-only deployment.","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"],"exampleFix":"// before\nnew MongoClient(uri, { readPreference: { mode: 'primary', maxStalenessSeconds: 5 } });\n// after\nnew MongoClient(uri, { readPreference: { mode: 'nearest', maxStalenessSeconds: 5 } });","handlingStrategy":"validation","validationCode":"const VALID_MODES = ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'];\nfunction isValidReadPreferenceObject(v) {\n  if (!v || typeof v !== 'object') return false;\n  if (!VALID_MODES.includes(v.mode)) return false;\n  if (v.mode === 'primary' && (v.maxStalenessSeconds != null || (v.tags && v.tags.length))) return false;\n  return true;\n}\nif (options.readPreference && typeof options.readPreference === 'object' && !isValidReadPreferenceObject(options.readPreference)) {\n  throw new TypeError('Invalid readPreference object');\n}","typeGuard":"const VALID_MODES = new Set(['primary','primaryPreferred','secondary','secondaryPreferred','nearest']);\nfunction isReadPreferenceMode(m) {\n  return VALID_MODES.has(m);\n}","tryCatchPattern":null,"preventionTips":["Use a valid mode string","Do not combine 'primary' with maxStalenessSeconds or tags","Pass a ReadPreference instance or a plain string for clarity"],"tags":["read-preference","configuration","options"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}