{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/read_preference.ts#L173-L209","documentation":"Thrown by `ReadPreference.translate` when `options.readPreference` is present but is neither a string, a ReadPreference instance, nor a plain object carrying a string `mode`/`preference` field. The translate helper is used to normalize read preference options before command execution (read_preference.ts:191). Whatever value was passed does not match any accepted shape.","triggerScenarios":"Setting `readPreference` to a number, boolean, array, or an object that lacks a `mode` (e.g. `{ tags: [...] }` with no mode); passing a BSON document or class instance where a read preference is expected.","commonSituations":"Typo in the option name so an unrelated value lands in `readPreference`; constructing options dynamically and forgetting the mode field; deserializing a read preference from JSON that omits `mode`.","solutions":["Provide a valid mode string ('primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest').","Pass a `ReadPreference` instance built via `new ReadPreference(mode)` or `ReadPreference.fromString(mode)`.","If using an object form, ensure it has a string `mode` (or `preference`) property."],"exampleFix":"// before\nfind({}, { readPreference: { tags: [{ region: 'eu' }] } });\n// after\nfind({}, { readPreference: { mode: 'nearest', tags: [{ region: 'eu' }] } });","handlingStrategy":"validation","validationCode":"function normalizeReadPref(rp) {\n  if (typeof rp === 'string' || rp instanceof ReadPreference) return rp;\n  if (rp && typeof rp === 'object' && typeof (rp.mode ?? rp.preference) === 'string') return rp;\n  throw new TypeError('readPreference must be a string, ReadPreference, or { mode: string }');\n}","typeGuard":"function isValidReadPrefInput(rp) {\n  if (rp == null) return true;\n  if (typeof rp === 'string') return true;\n  if (rp instanceof ReadPreference) return true;\n  return typeof rp === 'object' && typeof (rp.mode ?? rp.preference) === 'string';\n}","tryCatchPattern":null,"preventionTips":["Validate dynamic read preferences against the known mode set before use.","Prefer ReadPreference instances over ad-hoc objects.","Add a runtime assert in config-loading code."],"tags":["read-preference","configuration","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}