Mongoose in Next.js dev: 'OverwriteModelError: Cannot overwrite model once compiled'
@devsingh05▲ 10 net votes
Hot reload re-runs the module that calls mongoose.model('User', schema), and Mongoose refuses to register the same name twice. Export export default mongoose.models.User || mongoose.model('User', userSchema). Cache the connection on globalThis for the same reason, so each reload does not open a new connection pool.