how to handle this mongoose error in next v14
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
why i am having this error while integrating mongoose in next v14?
7 Replies
ah I know this one
it's when you try to create multiple mongoDB instances
I'm guessing you have some sort of init function to initialise your db
what it returns (the mongoDB instance) needs to be defined out of the function and then the init function for MongoDB will only be run when that mongoDB insteance doesn't exist yet
You probably ahve something like this.
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
});
const UserSchema = new mongoose.Schema({
// My schema stuff
});
const User = mongoose.model("User", UserSchema);You need to save User outside of the function returning it so you can reuse it
hope this helps