{"record":{"id":"8d9e28e6992453d4","repo":"mongodb/node-mongodb-native","slug":"invalid-read-preference-r","errorCode":null,"errorMessage":"Invalid read preference: ${r}","messagePattern":"Invalid read preference: (.+?)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/read_preference.ts","lineNumber":191,"sourceCode":"\n  /**\n   * Replaces options.readPreference with a ReadPreference instance\n   */\n  static translate(options: ReadPreferenceLikeOptions): ReadPreferenceLikeOptions {\n    if (options.readPreference == null) return options;\n    const r = options.readPreference;\n\n    if (typeof r === 'string') {\n      options.readPreference = new ReadPreference(r);\n    } else if (r && !(r instanceof ReadPreference) && typeof r === 'object') {\n      const mode = r.mode || r.preference;\n      if (mode && typeof mode === 'string') {\n        options.readPreference = new ReadPreference(mode, r.tags, {\n          maxStalenessSeconds: r.maxStalenessSeconds\n        });\n      }\n    } else if (!(r instanceof ReadPreference)) {\n      throw new MongoInvalidArgumentError(`Invalid read preference: ${r}`);\n    }\n\n    return options;\n  }\n\n  /**\n   * Validate if a mode is legal\n   *\n   * @param mode - The string representing the read preference mode.\n   */\n  static isValid(mode: string): boolean {\n    const VALID_MODES = new Set([\n      ReadPreference.PRIMARY,\n      ReadPreference.PRIMARY_PREFERRED,\n      ReadPreference.SECONDARY,\n      ReadPreference.SECONDARY_PREFERRED,\n      ReadPreference.NEAREST,\n      null","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/read_preference.ts#L173-L209","documentation":"Thrown by ReadPreference.translate() (src/read_preference.ts:191) when options.readPreference is not a string, not a plain object, and not a ReadPreference instance. translate() converts a loosely-typed read preference value into a ReadPreference; any other type (number, array, boolean, null after the earlier null check) is rejected. MongoInvalidArgumentError.","triggerScenarios":"Passing options.readPreference = 2, options.readPreference = ['secondary'], options.readPreference = true, or a value of an unexpected type through translate().","commonSituations":"Deserialization from a config format that produced a non-string/non-object value, or a programming error passing the wrong variable.","solutions":["Pass readPreference as a string ('secondary'), an object ({ mode: 'secondary' }), or a ReadPreference instance.","Normalize config values before they reach the driver.","Use ReadPreference.fromOptions() which is more lenient with object shapes."],"exampleFix":"// before\nReadPreference.translate({ readPreference: 2 });\n// after\nReadPreference.translate({ readPreference: 'secondary' });","handlingStrategy":"type-guard","validationCode":"function normalizeRP(v) {\n  if (typeof v === 'string') return new ReadPreference(v);\n  if (v && typeof v === 'object' && typeof v.mode === 'string') return new ReadPreference(v.mode, v.tags, { maxStalenessSeconds: v.maxStalenessSeconds });\n  throw new Error('readPreference must be string, object, or ReadPreference');\n}","typeGuard":"function isReadPreferenceLike(v): v is string | { mode: string } | ReadPreference {\n  return typeof v === 'string' || (v != null && typeof v === 'object' && ('mode' in v || v instanceof ReadPreference));\n}","tryCatchPattern":"try {\n  ReadPreference.translate({ readPreference: rp });\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /Invalid read preference/.test(e.message)) {\n    // replace with a valid value\n  }\n  throw e;\n}","preventionTips":["Type readPreference fields as string | ReadPreference at API boundaries.","Avoid passing numeric or boolean values for readPreference.","Use ReadPreference.fromOptions for more lenient object handling."],"tags":["read-preference","validation","invalid-argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}