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

Fix "Invalid schema configuration: `model` is not a valid type within the array" Mongoose 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 },
// ...

Hope you found this post useful. It was published . Please show your love and support by sharing this post.