Automattic/mongoose · error · TypeError
A setter must be a function.
Error message
A setter must be a function.
What it means
SchemaType#set registers a setter function on a path and throws a TypeError for any non-function argument (lib/schemaType.js:829). Common triggers are passing a method name as a string, an undefined variable (import typo), or the already-invoked result of a function.
Source
Thrown at lib/schemaType.js:829
* const nameSchema = new Schema({ name: String, keywords: [String] });
* nameSchema.path('name').set(function(v) {
* // Need to check if `this` is a document, because in mongoose 5
* // setters will also run on queries, in which case `this` will be a
* // mongoose query object.
* if (this instanceof Document && v != null) {
* this.keywords = v.split(' ');
* }
* return v;
* });
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.set = function(fn) {
if (typeof fn !== 'function') {
throw new TypeError('A setter must be a function.');
}
this.setters.push(fn);
return this;
};
/**
* Adds a getter to this schematype.
*
* #### Example:
*
* function dob (val) {
* if (!val) return val;
* return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
* }
*
* // defining within the schema
* const s = new Schema({ born: { type: Date, get: dob })
*View on GitHub (pinned to 49cdab0136)
Solutions
- Pass a function reference: `schema.path('name').set(v => v.trim())`
- Guard optional setters: `if (typeof fn === 'function') path.set(fn)`
- Fix the import/variable name that resolved to undefined
Example fix
// before
schema.path('name').set('trim'); // throws TypeError
// after
schema.path('name').set(v => v.trim()); Defensive patterns
Strategy: type-guard
Validate before calling
function addSetter(schematype, fn) {
if (typeof fn !== 'function') {
throw new TypeError(`setter for ${schematype.path} must be a function, got ${typeof fn}`);
}
return schematype.set(fn);
} Type guard
const isFunction = v => typeof v === 'function';
Prevention
- Pass function references or arrow functions, never method-name strings
- Guard config-driven setters: if (typeof fn === 'function') path.set(fn)
- Check imports when a setter variable is mysteriously undefined
When it happens
Trigger: `schema.path('name').set('uppercase')`; `.set(opts.setter)` where `opts.setter` is undefined; `.set(transform.v)` where `v` is a bound-method lookup that failed silently.
Common situations: Config-driven setter wiring; optional chaining producing undefined (`config?.post`); renaming imports and missing one usage; copying setter names from docs as strings.
Related errors
- A getter must be a function.
- Arguments must be aggregate pipeline operators
- Union schema type requires an array of types
- Invalid addFields() argument. Must be an object
- Invalid project() argument. Must be string or object
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/c5dd201d564d5b35.
Report an issue: GitHub.