Next.js Discord

Discord Forum

Mongoose Connection Error NextJS

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Avatar
Brown bearOP
Code
connect-mongoose.js
import mongoose from "mongoose"
const connection = {
  status: 0
}
export default async function mongoConnect() {
  try {
    console.info("Establishing Database Connection...")
    if (connection.status > 0) {
      console.info("Database Already Connected.")
      return true
    }
    const database = await mongoose.connect(process.env.DB_STRING, {
      dbName: "SF",
      autoIndex: false,
      connectTimeoutMS: 6000
    })
    connection.status = database.connections[0].readyState
    console.log("Database Connected Successfully...")
    return true
  } catch (error) {
    console.error("Error While Establishing Connection With Database.", error)
    return false
  }
}


Error:
✓ Compiled /api/blogs in 481ms (159 modules)
Establishing Database Connection...
Error While Establishing Connection With Database. [TypeError: mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect is not a function]
[TypeError: _utils_models_blogs_model__WEBPACK_IMPORTED_MODULE_1__.Blogs.find is not a function]

1 Reply

Avatar
Transvaal lion
Hi, are you trying to connect to mongoose from the middleware?