How to Fix "`model` is not a valid type within the array" Mongoose Schema Error?

If you are getting the Mongoose error "Invalid schema configuration: `model` is not a valid type within the array" then it means that you are specifying model object where schema object is expected. For example, consider the following which would cause an error:

// OrderModel.js

// ...
const Order = model('order', orderSchema);

export default Order;
// UserModel.js
import Order from './OrderModel';

const userSchema = new Schema({
    id: { type: Schema.Types.ObjectId, required: false },
    name: { type: String, required: true },
    email: { type: String, required: true },
    // Error: Invalid schema configuration: `model` is not a valid type within the array
    orders: { type: [Order], required: true },
});

To fix this, you can either export/import the schema object and use that, or you could simply access the underlying schema from the model object, for example, like so:

// ...
    orders: { type: [Order.schema], required: true },
// ...

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.