{"id":"cccdfec9bad54362","repo":"mongodb/node-mongodb-native","slug":"invalid-read-preference-specified","errorCode":null,"errorMessage":"Invalid read preference specified","messagePattern":"Invalid read preference specified","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/sdam/server_selection.ts","lineNumber":385,"sourceCode":"    }\n\n    default: {\n      const _exhaustiveCheck: never = mode;\n      throw new MongoRuntimeError(\n        `unexpected readPreference=${mode} (should never happen).  Please report a bug in the Node driver Jira project.`\n      );\n    }\n  }\n}\n\n/**\n * Returns a function which selects servers based on a provided read preference\n *\n * @param readPreference - The read preference to select with\n */\nexport function readPreferenceServerSelector(readPreference: ReadPreference): ServerSelector {\n  if (!readPreference.isValid()) {\n    throw new MongoInvalidArgumentError('Invalid read preference specified');\n  }\n\n  return function readPreferenceServers(\n    topologyDescription: TopologyDescription,\n    servers: ServerDescription[],\n    deprioritized: DeprioritizedServers\n  ): ServerDescription[] {\n    switch (topologyDescription.type) {\n      case 'Single':\n        return latencyWindowReducer(topologyDescription, servers.filter(knownFilter));\n      case 'ReplicaSetNoPrimary':\n      case 'ReplicaSetWithPrimary':\n        return secondarySelector(readPreference, topologyDescription, servers, deprioritized);\n      case 'Sharded': {\n        const selectable = filterDeprioritized(servers, deprioritized);\n        return latencyWindowReducer(topologyDescription, selectable.filter(knownFilter));\n      }\n      case 'Unknown':","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sdam/server_selection.ts#L367-L403","documentation":"Thrown by `readPreferenceServerSelector` when `readPreference.isValid()` returns false (server_selection.ts:384). `isValid` checks that the mode is one of the five recognized strings (or null). Unlike the constructor, the selector re-validates, so an invalidly-constructed ReadPreference is caught here at selection time.","triggerScenarios":"Constructing `new ReadPreference('whatever')` (the constructor does not validate mode) and then running an operation that triggers server selection; passing a ReadPreference whose mode was mutated after construction.","commonSituations":"Assuming the constructor validates mode; building a ReadPreference from untrusted input without validation; typos in mode strings.","solutions":["Build read preferences via `ReadPreference.fromString(mode)` and check `ReadPreference.isValid(mode)` first.","Validate user-supplied mode strings against the known set before constructing a ReadPreference.","Avoid mutating an existing ReadPreference instance's mode field."],"exampleFix":"// before\nconst rp = new ReadPreference('secndary'); // typo, not validated\nfind({}, { readPreference: rp });\n// after\nconst mode = ReadPreference.isValid('secondary') ? 'secondary' : 'primary';\nfind({}, { readPreference: new ReadPreference(mode) });","handlingStrategy":"validation","validationCode":"function buildReadPref(mode) {\n  if (!ReadPreference.isValid(mode)) throw new TypeError('Invalid read preference mode: ' + mode);\n  return new ReadPreference(mode);\n}","typeGuard":"function isValidReadPref(mode) {\n  return ReadPreference.isValid(mode);\n}","tryCatchPattern":null,"preventionTips":["Call ReadPreference.isValid(mode) before constructing from external input.","Prefer ReadPreference.fromString for string inputs.","Do not mutate an existing ReadPreference.mode."],"tags":["read-preference","validation","server-selection"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}