{"id":"942eeb47b9f4fdf1","repo":"mongodb/node-mongodb-native","slug":"argument-for-maxawaittimems-must-be-a-number","errorCode":null,"errorMessage":"Argument for maxAwaitTimeMS must be a number","messagePattern":"Argument for maxAwaitTimeMS must be a number","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":342,"sourceCode":"   * Add a comment to the cursor query allowing for tracking the comment in the log.\n   *\n   * @param value - The comment attached to this query.\n   */\n  comment(value: string): this {\n    this.throwIfInitialized();\n    this.findOptions.comment = value;\n    return this;\n  }\n\n  /**\n   * Set a maxAwaitTimeMS on a tailing cursor query to allow to customize the timeout value for the option awaitData (Only supported on MongoDB 3.2 or higher, ignored otherwise)\n   *\n   * @param value - Number of milliseconds to wait before aborting the tailed query.\n   */\n  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;","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L324-L360","documentation":"Thrown by FindCursor.maxAwaitTimeMS() (MongoInvalidArgumentError) when the value argument is not of type 'number'. maxAwaitTimeMS controls how long each getMore on a tailable awaitData cursor blocks waiting for new data; a non-numeric value (string from env, undefined, NaN from arithmetic) is rejected.","triggerScenarios":"cursor.maxAwaitTimeMS('1000'), cursor.maxAwaitTimeMS(process.env.MAX_AWAIT) where the env var is unset/undefined, or cursor.maxAwaitTimeMS(NaN).","commonSituations":"Reading timeout values from environment variables or config without Number() coercion; passing a value through an any-typed helper that loses the type.","solutions":["Coerce with Number() and validate: const ms = Number(val); if (Number.isFinite(ms)) cursor.maxAwaitTimeMS(ms)","Use a typed config loader (zod, envalid) that guarantees number output","Pass a numeric literal or constant"],"exampleFix":"// before\ncursor.maxAwaitTimeMS(process.env.MAX_AWAIT_MS); // undefined\n// after\nconst ms = Number(process.env.MAX_AWAIT_MS ?? 1000);\ncursor.maxAwaitTimeMS(ms);","handlingStrategy":"type-guard","validationCode":"function maxAwaitSafe(cursor, val) {\n  const ms = Number(val);\n  if (!Number.isFinite(ms)) throw new TypeError('maxAwaitTimeMS must be a finite number');\n  return cursor.maxAwaitTimeMS(ms);\n}","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":null,"preventionTips":["Coerce env/config values with Number() before passing","Type config objects so maxAwaitTimeMS is number","Validate Number.isFinite() for arithmetic-derived values"],"tags":["find","tailable","timeout","validation","typescript"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}