Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mongoose throws Invalid schema configuration when the schema is created

ErrorTypeError: Invalid schema configuration: `...` is not a valid type at path `...`.

Short answer

Mongoose throws this TypeError while building a Schema, before any database call. The message names the path and the value it could not read as a type. The usual causes are a plain value used in place of a type, a type name Mongoose does not have, or an array whose element is undefined.

  • Applies to Mongoose 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the path in backticks in the message; it is the key that Mongoose could not turn into a type. Open that line of the schema and look at the value on the right.

    node -e "require('./models/user.js')" 2>&1 | head -n 5
  2. 2Match what you see, then jump to its fix

    1. 1`...` is not a valid type at path `age`A plain value or an unknown name was written where a type belongsFix
    2. 2Could not determine the embedded type for array `tags`An array element type is undefinedFix
    3. 3`...` is not a valid type within the array `...`The array element is not a type Mongoose knowsFix

What does the error look like?

  • TypeError: Invalid schema configuration: `...` is not a valid type at path `...`.
  • Invalid schema configuration: Could not determine the embedded type for array `...`.
  • Invalid schema configuration: `...` is not a valid type within the array `...`.

Cause 1 of 3

A plain value or an unknown name was written where a type belongs

1Check

In Schema.add, a key whose value is neither an object nor a function (and not the name of a Mongoose type) is rejected with this message. Writing age: 18, active: true or a misspelled type name triggers it. A typical slip is a default value written in place of the type.

grep -n -B1 -A12 "new Schema\|new mongoose.Schema" models/user.js

2Fix

Give the path a real SchemaType, and move the value into a default. Use a name from the Mongoose list: String, Number, Date, Buffer, Boolean, Mixed, ObjectId, Array, Decimal128, Map, UUID, BigInt and others.

const userSchema = new mongoose.Schema({
  name: String,
  age: { type: Number, default: 18 },
  active: { type: Boolean, default: true },
});

3Verify

Load the model file again; it should run with no error.

node -e "require('./models/user.js'); console.log('schema ok')"

Confirmed by mongoosejs.com

Did this fix it?

Cause 2 of 3

An array element type is undefined

1Check

The array branch of the schema parser fails when the element type resolves to undefined. That happens when the sub-schema or type you wrote inside [ ] is undefined at that moment, for example a misspelled import or a value that is not exported.

node -e "const s = require('./models/child.js'); console.log(typeof s)"

2Fix

Make sure the element is defined before the parent schema uses it: export the sub-schema from its own file and import it at the top of the parent.

// child.schema.js
const childSchema = new mongoose.Schema({ label: String });
module.exports = childSchema;

// parent.js
const childSchema = require('./child.schema');
const parentSchema = new mongoose.Schema({ children: [childSchema] });

3Verify

Load the parent model file.

node -e "require('./models/parent.js'); console.log('schema ok')"

Confirmed by mongoosejs.com

Did this fix it?

Cause 3 of 3

The array element is not a type Mongoose knows

1Check

The element type name is looked up among the registered Mongoose types. A made-up or misspelled element type is not found.

grep -n "\[" models/*.js | grep -v "\[\]"

2Fix

Use a SchemaType (String, Number, ObjectId) or a sub-schema inside the array.

const postSchema = new mongoose.Schema({
  tags: [String],
  authors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
});

3Verify

Load the model file and create one document in a script.

node -e "const m=require('./models/post.js'); console.log(new m({tags:['a']}).tags)"

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Write every path as { type: ..., default: ... } once it needs more than a type.
  • Put sub-schemas in their own files and import them at the top of the parent.
  • Load every model once at start-up in a test, so a broken schema fails the build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.