{"record":{"id":"0b9f57d831d95d2d","repo":"mongodb/node-mongodb-native","slug":"unknown-readpreference-value-value","errorCode":null,"errorMessage":"Unknown ReadPreference value: ${value}","messagePattern":"Unknown ReadPreference value: (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":1036,"sourceCode":"        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({\n      values,\n      options\n    }: {\n      values: Array<string | Record<string, string>[]>;\n      options: MongoClientOptions;\n    }) {\n      const tags: Array<string | Record<string, string>> = Array.isArray(values[0])\n        ? values[0]\n        : (values as Array<string>);\n      const readPreferenceTags = [];\n      for (const tag of tags) {\n        const readPreferenceTag: TagSet = Object.create(null);\n        if (typeof tag === 'string') {","sourceCodeStart":1018,"sourceCodeEnd":1054,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L1018-L1054","documentation":"The `readPreference` transform at src/connection_string.ts:1010 handles three shapes: a ReadPreference instance, a record with a `mode` field, or a plain string. Anything else falls through to the final throw at src/connection_string.ts:1036 (`Unknown ReadPreference value`).","triggerScenarios":"Passing `readPreference: 2`, `readPreference: true`, `readPreference: ['secondary']`, `readPreference: null`, or a tags-only object with no `mode` key.","commonSituations":"Passing a numeric priority (confusing with read preference integers in some clients); reusing a config field typed as `string | number`; passing a tags array directly under readPreference instead of `readPreferenceTags`.","solutions":["Pass a string mode: `readPreference: 'secondary'`.","Or an object with a mode: `readPreference: { mode: 'secondary' }`.","Move tag sets to the dedicated `readPreferenceTags` option."],"exampleFix":"// before\nnew MongoClient(uri, { readPreference: 2 });\n\n// after\nnew MongoClient(uri, { readPreference: 'secondary' });","handlingStrategy":"type-guard","validationCode":"import { ReadPreference } from 'mongodb';\nfunction isReadPreferenceInput(v: unknown): boolean {\n  return v instanceof ReadPreference || typeof v === 'string' || (!!v && typeof v === 'object' && 'mode' in v);\n}\nif (opts.readPreference != null && !isReadPreferenceInput(opts.readPreference)) {\n  throw new Error('readPreference must be a string, { mode }, or ReadPreference instance');\n}","typeGuard":"function isReadPreferenceInput(v: unknown): v is string | { mode: string } { return typeof v === 'string' || (!!v && typeof v === 'object' && 'mode' in v); }","tryCatchPattern":"try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoParseError && /Unknown ReadPreference/.test(e.message)) { opts.readPreference = 'primary'; client = new MongoClient(uri, opts); } else throw e; }","preventionTips":["Type readPreference as a union: string | { mode: ReadPreferenceMode } | ReadPreference.","Send tag sets through `readPreferenceTags`, not inside readPreference.","Reject numeric read preference values at the config boundary."],"tags":["read-preference","configuration","type-error"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}