Next.js Discord

Discord Forum

NextJS API folder with Mongoose & Middleware

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Hi all,

My stack is NextJS 14 with App directory and API folder for my mobile app to call my api route.ts
I want some pieces of functions to be called on every route, so I am using Middleware

However, mongoose drive fails to work in middleware

getting the following error, whats the work around for this ?
mongooseWEBPACK_IMPORTED_MODULE0default(...).connect is not a function

Please let me know

1 Reply

Transvaal lion
Hi, mongoose doesn’t support the nextjs edge runtime, you can import it but it won’t be able to create a connection. This is because the edge runtime doesn’t support the net api from node which is used by the MongoDB node driver to connect to a db. Hope this helps!