{"record":{"id":"458ac88f9c5383d9","repo":"mongodb/node-mongodb-native","slug":"invalid-read-preference-mode-json-stringify-mode","errorCode":null,"errorMessage":"Invalid read preference mode ${JSON.stringify(mode)}","messagePattern":"Invalid read preference mode (.+?)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/read_preference.ts","lineNumber":86,"sourceCode":"  public static PRIMARY_PREFERRED = ReadPreferenceMode.primaryPreferred;\n  public static SECONDARY = ReadPreferenceMode.secondary;\n  public static SECONDARY_PREFERRED = ReadPreferenceMode.secondaryPreferred;\n  public static NEAREST = ReadPreferenceMode.nearest;\n\n  public static primary = new ReadPreference(ReadPreferenceMode.primary);\n  public static primaryPreferred = new ReadPreference(ReadPreferenceMode.primaryPreferred);\n  public static secondary = new ReadPreference(ReadPreferenceMode.secondary);\n  public static secondaryPreferred = new ReadPreference(ReadPreferenceMode.secondaryPreferred);\n  public static nearest = new ReadPreference(ReadPreferenceMode.nearest);\n\n  /**\n   * @param mode - A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest)\n   * @param tags - A tag set used to target reads to members with the specified tag(s). tagSet is not available if using read preference mode primary.\n   * @param options - Additional read preference options\n   */\n  constructor(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions) {\n    if (!ReadPreference.isValid(mode)) {\n      throw new MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`);\n    }\n    if (options == null && typeof tags === 'object' && !Array.isArray(tags)) {\n      options = tags;\n      tags = undefined;\n    } else if (tags && !Array.isArray(tags)) {\n      throw new MongoInvalidArgumentError('ReadPreference tags must be an array');\n    }\n\n    this.mode = mode;\n    this.tags = tags;\n    this.hedge = options?.hedge;\n    this.maxStalenessSeconds = undefined;\n\n    options = options ?? {};\n    if (options.maxStalenessSeconds != null) {\n      if (options.maxStalenessSeconds <= 0) {\n        throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer');\n      }","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/read_preference.ts#L68-L104","documentation":"Thrown by the ReadPreference constructor (src/read_preference.ts:86) when the mode is not one of the five valid values (primary, primaryPreferred, secondary, secondaryPreferred, nearest) or null. ReadPreference.isValid() builds a Set of the canonical modes; anything else raises MongoInvalidArgumentError with the JSON-stringified mode.","triggerScenarios":"Constructing new ReadPreference('secondary-only'), new ReadPreference(2), new ReadPreference('SECONDARY') (case-sensitive), or passing a bad mode via options.readPreference on connect or per-operation options.","commonSituations":"Typos in read preference strings, case mismatches ('Primary' vs 'primary'), or passing a numeric/symbol from a different driver's convention.","solutions":["Use one of the five lowercase canonical modes or a ReadPreference static instance (ReadPreference.secondary).","If reading from options, validate the value against the canonical set before constructing.","Use ReadPreference.fromString() only for trusted input; it does not validate the mode."],"exampleFix":"// before\nconst rp = new ReadPreference('SECONDARY');\n// after\nconst rp = new ReadPreference('secondary');\n// or\nconst rp = ReadPreference.secondary;","handlingStrategy":"validation","validationCode":"const VALID_MODES = new Set(['primary','primaryPreferred','secondary','secondaryPreferred','nearest']);\nfunction makeReadPreference(mode) {\n  if (!VALID_MODES.has(mode)) throw new Error(`bad mode: ${mode}`);\n  return new ReadPreference(mode);\n}","typeGuard":"function isReadPreferenceMode(m): m is 'primary'|'primaryPreferred'|'secondary'|'secondaryPreferred'|'nearest' {\n  return typeof m === 'string' && ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'].includes(m);\n}","tryCatchPattern":"try {\n  const rp = new ReadPreference(mode);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /Invalid read preference mode/.test(e.message)) {\n    // fall back to a safe default\n  }\n  throw e;\n}","preventionTips":["Use the static instances (ReadPreference.secondary) instead of string literals where possible.","Validate mode strings from configuration against the canonical set.","Remember mode strings are lowercase and case-sensitive."],"tags":["read-preference","validation","invalid-argument","sdam"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}