{"id":"b65a76ca667f2843","repo":"mongodb/node-mongodb-native","slug":"expected-name-to-be-stringified-int-value-got","errorCode":null,"errorMessage":"Expected ${name} to be stringified int value, got: ${value}","messagePattern":"Expected (.+?) to be stringified int value, got: (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":193,"sourceCode":"}\nfunction getBoolean(name: string, value: unknown): boolean {\n  if (typeof value === 'boolean') return value;\n  switch (value) {\n    case 'true':\n      return true;\n    case 'false':\n      return false;\n    default:\n      throw new MongoParseError(`${name} must be either \"true\" or \"false\"`);\n  }\n}\n\nfunction getIntFromOptions(name: string, value: unknown): number {\n  const parsedInt = parseInteger(value);\n  if (parsedInt != null) {\n    return parsedInt;\n  }\n  throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);\n}\n\nfunction getUIntFromOptions(name: string, value: unknown): number {\n  const parsedValue = getIntFromOptions(name, value);\n  if (parsedValue < 0) {\n    throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);\n  }\n  return parsedValue;\n}\n\nfunction* entriesFromString(value: string): Generator<[string, string]> {\n  if (value === '') {\n    return;\n  }\n  const keyValuePairs = value.split(',');\n  for (const keyValue of keyValuePairs) {\n    const [key, value] = keyValue.split(/:(.*)/);\n    if (value == null) {","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L175-L211","documentation":"Thrown by getIntFromOptions() when a URI option expected to be an integer cannot be parsed as one. parseInteger rejects non-numeric strings, booleans, objects, NaN, Infinity, and non-integer numbers. The error message names the option and shows the offending value. It is a MongoParseError.","triggerScenarios":"URI like ?maxPoolSize=ten or ?socketTimeoutMS=30s or ?connectTimeoutMS=2.5 (a float that parseInteger rejects) or ?serverSelectionTimeoutMS= (empty); passing a value with units like '100ms' that the driver does not interpret.","commonSituations":"Templating a numeric env var that is unset (empty string) or has units; copy-pasting values with units from docs of other drivers; locale-specific formatting inserting commas/decimals; JSON config feeding stringified floats.","solutions":["Provide a plain integer string, e.g. ?maxPoolSize=10 (milliseconds for timeout options).","Strip units and convert before interpolating: String(Number(value)).","Default unset numeric env vars to an integer constant.","Pass numeric options via the options object as real numbers to avoid string parsing pitfalls."],"exampleFix":"// before\nconst uri = `mongodb://h/db?connectTimeoutMS=${process.env.TIMEOUT ?? '5s'}`;\n// after\nconst timeoutMs = Number(process.env.TIMEOUT ?? 5000);\nnew MongoClient('mongodb://h/db', { serverSelectionTimeoutMS: timeoutMs });","handlingStrategy":"validation","validationCode":"function toIntUriValue(name: string, value: unknown): string {\n  const n = Number(value);\n  if (!Number.isFinite(n) || !Number.isInteger(n)) {\n    throw new Error(`Option ${name} must be an integer, got: ${String(value)}`);\n  }\n  return String(n);\n}\nconst uri = `mongodb://h/db?maxPoolSize=${toIntUriValue('maxPoolSize', process.env.MAX_POOL ?? 10)}`;","typeGuard":"const isIntegerLike = (v: unknown): v is number | string => {\n  const n = Number(v);\n  return Number.isFinite(n) && Number.isInteger(n);\n};","tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoParseError && /Expected .* to be stringified int/.test(e.message)) {\n    throw new Error('A numeric URI option was not a valid integer');\n  }\n  throw e;\n}","preventionTips":["Do not include units (ms, s) in numeric URI options — they are raw integers.","Default unset numeric env vars to an integer constant.","Prefer the options object with real numbers for numeric settings."],"tags":["connection-string","parsing","config","integer"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}