{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L1005-L1041","documentation":"When `readPreference` is an object, the transform at src/connection_string.ts:1017 first requires it to be a record with a `mode` field, then calls `ReadPreference.fromOptions`; if fromOptions returns null, MongoParseError is thrown at src/connection_string.ts:1023. This typically means the `mode` value is not a recognized ReadPreferenceMode.","triggerScenarios":"Passing `readPreference: { mode: 'primary-only' }`, `{ mode: 'standalone' }`, `{ mode: 'PRIMARY' }` (case mismatch), or any mode that is not one of primary, primaryPreferred, secondary, secondaryPreferred, nearest.","commonSituations":"Case-sensitivity mistakes (uppercase from another SDK); invented mode names; copying tags-only objects without a valid mode; merging configs that leave mode undefined inside the spread.","solutions":["Use a valid mode: primary, primaryPreferred, secondary, secondaryPreferred, or nearest.","Pass a plain string: `readPreference: 'secondary'` instead of an object when no tags/hedge are needed.","Confirm the merged object still has a `mode` field after any config spread."],"exampleFix":"// before\nnew MongoClient(uri, { readPreference: { mode: 'primary-only' } });\n\n// after\nnew MongoClient(uri, { readPreference: 'primary' });","handlingStrategy":"validation","validationCode":"const MODES = ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'];\nif (opts.readPreference && typeof opts.readPreference === 'object' && 'mode' in opts.readPreference) {\n  if (!MODES.includes((opts.readPreference as any).mode)) {\n    throw new Error(`invalid readPreference mode: ${(opts.readPreference as any).mode}`);\n  }\n}","typeGuard":"function isReadPreferenceMode(v: unknown): v is string { return typeof v === 'string' && ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'].includes(v); }","tryCatchPattern":"try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoParseError && /read preference/.test(e.message)) { opts.readPreference = 'primary'; client = new MongoClient(uri, opts); } else throw e; }","preventionTips":["Lowercase mode strings exactly as the enum values.","Prefer passing readPreference as a plain string when you have no tags.","Add a test asserting your config survives a config-merge round trip."],"tags":["read-preference","configuration"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}