Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Can I access Database in middleware function in _middleware.js in Next.js 12?

In Next.js 12, when I tried to connect mongodb in middleware function in _middleware.js under /pages/api/user folder, I got this error: TypeError: mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect is not a function at dbConnect (webpack-internal:///./src/server/utils/dbConnect.js:39:74) at Object.middleware [as handler] (webpack-internal:///./src/pages/api/user/_middleware.js:19:82) at adapter (webpack-internal:///./node_modules/next/dist/server/web/adapter.js:30:35)

part of my connection code like this:

import mongoose from "mongoose";
...
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
     return mongoose;
});

So I guess that Accessing Database in _middleware.js is prohibited. Am I right?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!