Member-only story

Cannot overwrite model once compiled Mongoose Nextjs/React(2021)

--

I met with this cannot overwrite model once compiled issue recently when working with a MERN app.
I got this error when trying to populate a model which has links to the models using refs .

If you would like to support me as a writer consider signing up to become a Medium member. It’s just $5 a month and you get unlimited access to Medium.

I came up with a very simple fix for that. Without using require to import the model. you can directly import the model like this inside controller.

var mongoose = require('mongoose');
var Blog = mongoose.model('Blog')

This fixed my issue

--

--

Lanka Rathnayaka
Lanka Rathnayaka

Responses (1)