{"record":{"id":"871f7ab927c763e7","repo":"Automattic/mongoose","slug":"cannot-convert-value-to-bigint-val","errorCode":null,"errorMessage":"Cannot convert value to BigInt: \"${val}\"","messagePattern":"Cannot convert value to BigInt: \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/cast/bigint.js","lineNumber":45,"sourceCode":"    if (val > MAX_BIGINT || val < MIN_BIGINT) {\n      throw new Error(ERROR_MESSAGE);\n    }\n    return val;\n  }\n\n  if (val instanceof Long) {\n    return val.toBigInt();\n  }\n\n  if (typeof val === 'string' || typeof val === 'number') {\n    val = BigInt(val);\n    if (val > MAX_BIGINT || val < MIN_BIGINT) {\n      throw new Error(ERROR_MESSAGE);\n    }\n    return val;\n  }\n\n  throw new Error(`Cannot convert value to BigInt: \"${val}\"`);\n};\n","sourceCodeStart":27,"sourceCodeEnd":47,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/cast/bigint.js#L27-L47","documentation":"castBigInt accepts null and '' (both become null), bigint, BSON Long, and string/number via BigInt(); every other type reaches the final throw with the value interpolated into the message. Arrays, plain objects, booleans, and exotic instances therefore cannot populate a BigInt path without explicit conversion.","triggerScenarios":"doc.big = { value: '123' }; doc.big = [123n]; doc.big = true; assigning a custom class instance or a Date to a BigInt path.","commonSituations":"Deserialized JSON objects assigned where a scalar was expected; wrapper types from form/ORM layers; passing ObjectId or Date by mistake; config parsed into structured values.","solutions":["Pass a supported type: bigint, numeric string, number, or BSON Long from the mongodb/bson package","Extract the primitive before assignment: doc.big = raw.value","Add a custom setter on the path if wrapped types must be accepted"],"exampleFix":"// before\ndoc.big = { value: '123' };\n\n// after\ndoc.big = '123'; // or 123n, or new Long(123)","handlingStrategy":"type-guard","validationCode":"const { Long } = require('mongodb');\nfunction isBigIntCastable(v) {\n  return v == null || v === '' ||\n    typeof v === 'bigint' || typeof v === 'string' ||\n    typeof v === 'number' || v instanceof Long;\n}\nif (!isBigIntCastable(input)) {\n  throw new TypeError(`Not castable to BigInt: ${typeof input}`);\n}","typeGuard":"const { Long } = require('mongodb');\nfunction isBigIntCastable(v) {\n  return v == null || v === '' ||\n    typeof v === 'bigint' || typeof v === 'string' ||\n    typeof v === 'number' || v instanceof Long;\n}","tryCatchPattern":"try {\n  doc.big = raw;\n  await doc.save();\n} catch (err) {\n  if (err.message.startsWith('Cannot convert value to BigInt')) {\n    // the message shows the exact value; convert it explicitly and re-save\n  }\n  throw err;\n}","preventionTips":["Type the field as bigint | number | string in TypeScript","Normalize request bodies with a coercion layer before save","Beware booleans -- they are not silently treated as 0/1 on BigInt paths"],"tags":["mongoose","bigint","cast","type-conversion"],"backgroundTag":"invalid-type-conversion","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}