{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L1018-L1054","documentation":"`readPreference` must be a `ReadPreference` instance, an object with a `mode` key, or a string. If it is none of these (number, boolean, array, null), the transform throws 'Unknown ReadPreference value' at src/connection_string.ts:1036.","triggerScenarios":"`{ readPreference: 2 }`; `{ readPreference: true }`; `{ readPreference: null }`; `{ readPreference: ['secondary'] }`.","commonSituations":"Config typos; passing a numeric priority; passing null intentionally but hitting the type check; array from a parsed query string.","solutions":["Set readPreference to a string mode like 'secondary'","Or an object `{ mode: 'secondary' }`","Or a ReadPreference instance","Omit the option to default to 'primary'"],"exampleFix":"// before\nnew MongoClient(uri, { readPreference: 2 });\n// after\nnew MongoClient(uri, { readPreference: 'secondary' });","handlingStrategy":"validation","validationCode":"import { ReadPreference } from 'mongodb';\nfunction isReadPreferenceInput(v) {\n  return (\n    v instanceof ReadPreference ||\n    typeof v === 'string' ||\n    (v != null && typeof v === 'object' && 'mode' in v)\n  );\n}\nif (options.readPreference != null && !isReadPreferenceInput(options.readPreference)) {\n  throw new TypeError('readPreference must be a string, { mode }, or ReadPreference');\n}","typeGuard":"import { ReadPreference } from 'mongodb';\nfunction isReadPreferenceLike(v) {\n  return v instanceof ReadPreference || typeof v === 'string' || (!!v && typeof v === 'object' && 'mode' in v);\n}","tryCatchPattern":null,"preventionTips":["Pass a string mode or { mode } object","Avoid numbers/booleans/arrays/null","Omit the option to default to 'primary'"],"tags":["read-preference","configuration","options"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}