{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/read_preference.ts#L68-L104","documentation":"Thrown by the ReadPreference constructor when the mode is not one of 'primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest' (or null). ReadPreference.isValid rejects any other string/value. MongoInvalidArgumentError, raised wherever a ReadPreference is constructed - directly, via fromOptions, fromString, or translate (e.g., parsing a readPreference from connection options or per-operation options).","triggerScenarios":"Passing a typo'd or unsupported mode string such as 'secondry', 'prefer-secondary', 'nearestRead', or a numeric/boolean value; setting readPreference in the URI or options to an invalid token; constructing new ReadPreference('random').","commonSituations":"Bad URI query param (?readPreference=...); environment-variable-driven config with a typo; code computing a mode string from user input without validation; mixing camelCase ('secondaryPreferred') vs different separators.","solutions":["Use one of the documented modes: primary, primaryPreferred, secondary, secondaryPreferred, nearest.","Use the ReadPreference static instances (ReadPreference.secondary) or ReadPreferenceMode constants to avoid typos.","Validate user/config-supplied mode strings against the allowed set before constructing a ReadPreference.","Check connection-string readPreference tokens for typos."],"exampleFix":"// before\nnew ReadPreference('secondry');\n// after\nnew ReadPreference('secondary');\n// or\nReadPreference.secondary;","handlingStrategy":"validation","validationCode":"import { ReadPreferenceMode } from 'mongodb';\nconst validModes = new Set<string>(Object.values(ReadPreferenceMode));\nif (!validModes.has(mode)) throw new Error(`readPreference mode invalid: ${mode}`);\nconst rp = new ReadPreference(mode as any);","typeGuard":"import { ReadPreferenceMode } from 'mongodb';\nfunction isReadPreferenceMode(v: unknown): v is (typeof ReadPreferenceMode)[keyof typeof ReadPreferenceMode] {\n  return typeof v === 'string' && Object.values(ReadPreferenceMode).includes(v as any);\n}","tryCatchPattern":null,"preventionTips":["Use ReadPreference static instances or ReadPreferenceMode constants instead of literals.","Validate mode strings parsed from config/URIs against the allowed set.","Watch for typos in connection-string readPreference tokens."],"tags":["read-preference","validation","argument-error","config"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}