{"id":"5d07c1ee42e8c83c","repo":"mongodb/node-mongodb-native","slug":"invalid-read-preference-readpreference","errorCode":null,"errorMessage":"Invalid read preference: ${readPreference}","messagePattern":"Invalid read preference: (.+?)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/abstract_cursor.ts","lineNumber":760,"sourceCode":"      this.transform = transform;\n    }\n\n    return this as unknown as AbstractCursor<T>;\n  }\n\n  /**\n   * Set the ReadPreference for the cursor.\n   *\n   * @param readPreference - The new read preference for the cursor.\n   */\n  withReadPreference(readPreference: ReadPreferenceLike): this {\n    this.throwIfInitialized();\n    if (readPreference instanceof ReadPreference) {\n      this.cursorOptions.readPreference = readPreference;\n    } else if (typeof readPreference === 'string') {\n      this.cursorOptions.readPreference = ReadPreference.fromString(readPreference);\n    } else {\n      throw new MongoInvalidArgumentError(`Invalid read preference: ${readPreference}`);\n    }\n\n    return this;\n  }\n\n  /**\n   * Set the ReadPreference for the cursor.\n   *\n   * @param readPreference - The new read preference for the cursor.\n   */\n  withReadConcern(readConcern: ReadConcernLike): this {\n    this.throwIfInitialized();\n    const resolvedReadConcern = ReadConcern.fromOptions({ readConcern });\n    if (resolvedReadConcern) {\n      this.cursorOptions.readConcern = resolvedReadConcern;\n    }\n\n    return this;","sourceCodeStart":742,"sourceCodeEnd":778,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/abstract_cursor.ts#L742-L778","documentation":"Thrown by cursor.withReadPreference(readPreference) when the argument is neither a ReadPreference instance nor a string. ReadPreferenceLike is the union of those two; any other type (number, object without the right shape, symbol) is rejected.","triggerScenarios":"Calling withReadPreference({ mode: 'primary' }) (plain object instead of instance), withReadPreference(2), withReadPreference(null), or withReadPreference(['secondary']). The string branch only accepts values that ReadPreference.fromString can parse.","commonSituations":"Passing a JSON-parsed config object that looks like a ReadPreference but is not an instance; passing a tag-set object directly; confusion between readPreference (the option) and ReadPreference (the class).","solutions":["Pass a string: withReadPreference('secondary').","Pass a ReadPreference instance: withReadPreference(new ReadPreference('secondary', [{ region: 'us-east' }])).","Set readPreference at the find()/aggregate() options level instead of mutating the cursor."],"exampleFix":"// before\ncursor.withReadPreference({ mode: 'secondary' });\n// after\ncursor.withReadPreference('secondary');","handlingStrategy":"type-guard","validationCode":"import { ReadPreference } from 'mongodb';\nfunction isReadPrefLike(v) { return v instanceof ReadPreference || typeof v === 'string'; }","typeGuard":"import type { ReadPreferenceLike } from 'mongodb';\nfunction isReadPreferenceLike(v): v is ReadPreferenceLike {\n  return v instanceof ReadPreference || typeof v === 'string';\n}","tryCatchPattern":null,"preventionTips":["Prefer passing a string ('primary'|'primaryPreferred'|'secondary'|'secondaryPreferred'|'nearest').","Set readPreference via find()/aggregate() options, not by mutating the cursor.","For tag sets, construct new ReadPreference(mode, tags) explicitly."],"tags":["cursor","read-preference","options","type-validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}