{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L324-L360","documentation":"Thrown by FindCursor.maxAwaitTimeMS when its argument is not of type 'number'. maxAwaitTimeMS controls how long the server blocks on a getMore for a tailable/awaitData cursor awaiting new data; the value must be a numeric millisecond count. A non-number (string, undefined after bad destructuring) is rejected synchronously.","triggerScenarios":"cursor.maxAwaitTimeMS('1000'); reading the value from config as a string and passing it uncoerced; cursor.maxAwaitTimeMS(someUndefinedVar); passing a value typed as number | undefined without a guard.","commonSituations":"Reading timeout values from environment variables (always strings) without Number(); JSON config files storing numbers as strings; optional config fields destructured with a default that resolves to undefined.","solutions":["Coerce the value explicitly: cursor.maxAwaitTimeMS(Number(value)).","Guard with typeof before calling: if (typeof v === 'number') cursor.maxAwaitTimeMS(v).","Fix the config source so the value is a real number, not a string."],"exampleFix":"// before\ncursor.maxAwaitTimeMS(process.env.MAX_AWAIT_MS);\n\n// after\ncursor.maxAwaitTimeMS(Number(process.env.MAX_AWAIT_MS));","handlingStrategy":"type-guard","validationCode":"function setMaxAwait(cursor, value) {\n  const ms = Number(value);\n  if (!Number.isFinite(ms)) throw new TypeError('maxAwaitTimeMS must be a finite number');\n  return cursor.maxAwaitTimeMS(ms);\n}","typeGuard":"function isFiniteNumber(v) { return typeof v === 'number' && Number.isFinite(v); }","tryCatchPattern":null,"preventionTips":["Always coerce env/config strings with Number() before passing to timeout setters.","Validate finiteness, not just typeof, to reject NaN."],"tags":["find","tailable","timeout","type-validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}