{"record":{"id":"8b71011b807b8410","repo":"Automattic/mongoose","slug":"invalid-subtype-expected-a-number","errorCode":null,"errorMessage":"Invalid subtype. Expected a number","messagePattern":"Invalid subtype\\. Expected a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/types/buffer.js","lineNumber":278,"sourceCode":" *     bson.BSON_BINARY_SUBTYPE_DEFAULT\n *     bson.BSON_BINARY_SUBTYPE_FUNCTION\n *     bson.BSON_BINARY_SUBTYPE_BYTE_ARRAY\n *     bson.BSON_BINARY_SUBTYPE_UUID\n *     bson.BSON_BINARY_SUBTYPE_MD5\n *     bson.BSON_BINARY_SUBTYPE_USER_DEFINED\n *\n *     doc.buffer.subtype(bson.BSON_BINARY_SUBTYPE_UUID);\n *\n * @see bsonspec https://bsonspec.org/#/specification\n * @param {Hex} subtype\n * @api public\n * @method subtype\n * @memberOf MongooseBuffer\n */\n\nMongooseBuffer.mixin.subtype = function(subtype) {\n  if (typeof subtype !== 'number') {\n    throw new TypeError('Invalid subtype. Expected a number');\n  }\n\n  if (this._subtype !== subtype) {\n    this._markModified();\n  }\n\n  this._subtype = subtype;\n};\n\n/*!\n * Module exports.\n */\n\nMongooseBuffer.Binary = Binary;\n\nmodule.exports = MongooseBuffer;\n","sourceCodeStart":260,"sourceCodeEnd":295,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/types/buffer.js#L260-L295","documentation":"MongooseBuffer.subtype() sets the BSON binary subtype of the buffer and validates its input: it must be a number (0-255 per the BSON spec). Passing a string like '4', null, or undefined throws a TypeError instead of storing an invalid subtype.","triggerScenarios":"doc.bin.subtype('4') with a numeric string from config/env; doc.bin.subite(undefined) via a typo'd or unset variable; passing the string constant instead of bson.BSON_BINARY_SUBTYPE_UUID.","commonSituations":"Reading the subtype from environment variables or JSON config (always strings); passing values from untyped request payloads.","solutions":["Pass a number literal: doc.bin.subtype(4)","Coerce config strings: doc.bin.subtype(Number(process.env.BIN_SUBTYPE))","Use the BSON constant: import { BSON_BINARY_SUBTYPE_UUID } from 'bson'"],"exampleFix":"// before\ndoc.bin.subtype(process.env.BIN_SUBTYPE); // string '4'\n// after\ndoc.bin.subtype(Number(process.env.BIN_SUBTYPE));","handlingStrategy":"type-guard","validationCode":"function setSubtype(buf, subtype) {\n  if (typeof subtype !== 'number' || !Number.isInteger(subtype) || subtype < 0 || subtype > 255) {\n    throw new TypeError('BSON subtype must be an integer 0-255');\n  }\n  buf.subtype(subtype);\n}","typeGuard":"function isValidSubtype(v) { return typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 255; }","tryCatchPattern":"try { buf.subtype(v); } catch (err) { if (/Expected a number/.test(err.message)) buf.subtype(Number(v)); else throw err; }","preventionTips":["Coerce config/env strings with Number() before passing to subtype()","Use bson's BSON_BINARY_SUBTYPE_* constants instead of magic values","Type the parameter as number in TS definitions to catch this at compile time"],"tags":["mongoose","buffer","subtype","typeerror","bson"],"backgroundTag":"invalid-argument-type","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}