{"id":"2bfe29184c8423bf","repo":"mongodb/node-mongodb-native","slug":"argument-for-maxtimems-must-be-a-number-2bfe29","errorCode":null,"errorMessage":"Argument for maxTimeMS must be a number","messagePattern":"Argument for maxTimeMS must be a number","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":357,"sourceCode":"  maxAwaitTimeMS(value: number): this {\n    this.throwIfInitialized();\n    if (typeof value !== 'number') {\n      throw new MongoInvalidArgumentError('Argument for maxAwaitTimeMS must be a number');\n    }\n\n    this.findOptions.maxAwaitTimeMS = value;\n    return this;\n  }\n\n  /**\n   * Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)\n   *\n   * @param value - Number of milliseconds to wait before aborting the query.\n   */\n  override maxTimeMS(value: number): this {\n    this.throwIfInitialized();\n    if (typeof value !== 'number') {\n      throw new MongoInvalidArgumentError('Argument for maxTimeMS must be a number');\n    }\n\n    this.findOptions.maxTimeMS = value;\n    return this;\n  }\n\n  /**\n   * Add a project stage to the aggregation pipeline\n   *\n   * @remarks\n   * In order to strictly type this function you must provide an interface\n   * that represents the effect of your projection on the result documents.\n   *\n   * By default chaining a projection to your cursor changes the returned type to the generic\n   * {@link Document} type.\n   * You should specify a parameterized type to have assertions on your final results.\n   *\n   * @example","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L339-L375","documentation":"Thrown by FindCursor.maxTimeMS() (MongoInvalidArgumentError) when value is not a number. maxTimeMS sets the server-side time limit for the initial find command; non-numeric input (string, undefined, object) is rejected before being sent.","triggerScenarios":"cursor.maxTimeMS('500'), cursor.maxTimeMS(undefined), cursor.maxTimeMS(config.maxTimeMS) where config field is a string.","commonSituations":"Config/env-driven timeouts parsed as strings; spreading an options object typed as any; copy-paste from JSON config.","solutions":["Coerce and validate: const ms = Number(val); if (Number.isFinite(ms)) cursor.maxTimeMS(ms)","Type your config objects so maxTimeMS is number","Use timeoutMS (the newer API) with a validated number instead"],"exampleFix":"// before\ncursor.maxTimeMS(cfg.timeoutStr);\n// after\ncursor.maxTimeMS(Number(cfg.timeoutStr));","handlingStrategy":"type-guard","validationCode":"function maxTimeMSSafe(cursor, val) {\n  const ms = Number(val);\n  if (!Number.isFinite(ms)) throw new TypeError('maxTimeMS must be a finite number');\n  return cursor.maxTimeMS(ms);\n}","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":null,"preventionTips":["Coerce string config values with Number()","Type config objects so maxTimeMS is number","Consider switching to timeoutMS with validated numbers"],"tags":["find","timeout","validation","typescript"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}