Next.js Discord

Discord Forum

Using Mongoose Model inside NextJS middleware results in undefined model error.

Palm Warbler posted this in #help-forum
Open in Discord
Palm WarblerOP
Hi, I have a dbConnect() method and a Mongoose model that I am able to use in API routes, but when I try to use these in a middleware, it throws an undefined model error:
- error src\models\User.ts (32:0) @ <unknown>
- error Cannot read properties of undefined (reading 'User')
>32| export default mongoose.models.User ||
33 |   mongoose.model("User",  UserSchema);

Any clue why this could be happening?

0 Replies