{"record":{"id":"3c009fc19981338c","repo":"Automattic/mongoose","slug":"a-getter-must-be-a-function","errorCode":null,"errorMessage":"A getter must be a function.","messagePattern":"A getter must be a function\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":899,"sourceCode":" *       name: { type: String, required: true, get: inspector },\n *       taxonomy: { type: String, get: inspector }\n *     })\n *\n *     const Virus = db.model('Virus', VirusSchema);\n *\n *     Virus.findById(id, function (err, virus) {\n *       console.log(virus.name);     // name is required\n *       console.log(virus.taxonomy); // taxonomy is not\n *     })\n *\n * @param {Function} fn\n * @return {SchemaType} this\n * @api public\n */\n\nSchemaType.prototype.get = function(fn) {\n  if (typeof fn !== 'function') {\n    throw new TypeError('A getter must be a function.');\n  }\n  this.getters.push(fn);\n  return this;\n};\n\n/**\n * Adds multiple validators for this document path.\n * Calls `validate()` for every element in validators.\n *\n * @param {(RegExp|Function|object)[]} validators\n * @returns {SchemaType}\n */\n\nSchemaType.prototype.validateAll = function(validators) {\n  for (let i = 0; i < validators.length; i++) {\n    this.validate(validators[i]);\n  }\n  return this;","sourceCodeStart":881,"sourceCodeEnd":917,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L881-L917","documentation":"SchemaType#get registers a getter function on a path and throws a TypeError for any non-function argument (lib/schemaType.js:899). Like the setter variant, it guards against strings, undefined variables, and other non-callables before pushing to the getters array.","triggerScenarios":"`schema.path('name').get('toJSON')`; `.get(mask)` where mask is undefined due to a missed import; passing a config object instead of a function.","commonSituations":"Config-driven getter wiring; optional features where the getter key is absent (`config.formatter` undefined); copy-paste of getter names as strings from documentation.","solutions":["Pass a function reference: `schema.path('name').get(v => v?.toUpperCase())`","Guard optional getters: `if (typeof fn === 'function') path.get(fn)`","Fix the import/variable name that resolved to undefined"],"exampleFix":"// before\nschema.path('dob').get('toISOString'); // throws TypeError\n\n// after\nschema.path('dob').get(v => (v ? v.toISOString() : v));","handlingStrategy":"type-guard","validationCode":"function addGetter(schematype, fn) {\n  if (typeof fn !== 'function') {\n    throw new TypeError(`getter for ${schematype.path} must be a function, got ${typeof fn}`);\n  }\n  return schematype.get(fn);\n}","typeGuard":"const isFunction = v => typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Pass function references or arrow functions, never method-name strings","Guard optional config getters before registering them","Prefer module-level named functions so stack traces stay readable"],"tags":["mongoose","schema","getter","typeerror","validation"],"backgroundTag":"callback-must-be-function","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}