DEV Community

Discussion on: Designing a better architecture for a Node.js API

Collapse
 
pacheco profile image
Thiago Pacheco • Edited

I ran some tests some time ago and I realize that when you try to get the model multiple times this error could show up because we try to reinitialize the model.

So a quick fix for that is to add a custom function to your model to get only the model and not reinitialize all of it, like the example below:

getModel() {
return mongoose.model("your model name");
}

Let me know if that solves your problem. :)

Collapse
 
allstackdev profile image
AllStackDev 👨‍💻

Yes wanted to do this but realized that it will be repeatation of code so I added a functionality to the
getInstance (init =true) {
init && initScheme();
return mongoose.model("your model name");
}

Thread Thread
 
pacheco profile image
Thiago Pacheco

This is way better, nice job man!
I am going to change the structure of this model soon.