{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L339-L375","documentation":"Thrown by FindCursor.maxTimeMS when its argument is not of type 'number'. maxTimeMS sets a server-side deadline on the find command; the driver validates the type before storing it on findOptions. Strings, booleans, undefined, or objects are rejected.","triggerScenarios":"cursor.maxTimeMS('5000'); cursor.maxTimeMS(parseInt(value)) where parseInt returned NaN; passing a config value typed as string | number without narrowing.","commonSituations":"Environment-variable-derived timeouts (strings); parseInt/Number returning NaN on bad input; spreading an options object whose maxTimeMS field is incorrectly typed.","solutions":["Coerce and validate: const ms = Number(value); if (!Number.isFinite(ms)) throw ...; cursor.maxTimeMS(ms).","Use the typed option in the find call: collection.find({}, { maxTimeMS: 5000 }).","Check for NaN explicitly because typeof NaN === 'number' passes the guard but is invalid downstream."],"exampleFix":"// before\nconst ms = parseInt(input); // NaN if input is bad\ncursor.maxTimeMS(ms);\n\n// after\nconst ms = Number(input);\nif (!Number.isFinite(ms)) throw new Error('maxTimeMS must be a finite number');\ncursor.maxTimeMS(ms);","handlingStrategy":"type-guard","validationCode":"function setMaxTimeMS(cursor, value) {\n  const ms = Number(value);\n  if (!Number.isFinite(ms)) throw new TypeError('maxTimeMS must be a finite number');\n  return cursor.maxTimeMS(ms);\n}","typeGuard":"function isFinitePositiveNumber(v) {\n  return typeof v === 'number' && Number.isFinite(v) && v > 0;\n}","tryCatchPattern":null,"preventionTips":["Check Number.isFinite rather than typeof, because typeof NaN === 'number'.","Pass maxTimeMS via find/aggregate options to keep validation centralized."],"tags":["find","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"}