Jump to content
  • 0

Mongoose model already compiled in Lolo IDE


Sofia
 Share

Question

Hey, I have a problem. When working with Mongoose in Lolo i have a problem with turning the schema into a model. I can use the model I created to fetch data once but then it crashes and gives me the error message:

"Cannot overwrite posts model once compiled"

Usually this would be fixed by using:

module.exports = mongoose.model('posts', schema);

In the js file with the schema and then:

const post = require("./schema.js")

In the file you want to use the schema in. How can this be achieved in the Lolo IDE? 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hello Sofia! Since we are working with nodes and not files we cannot call another node from a node. However to solve your problem in the Lolo IDE I recommend using something like this:

  const mongooseModel = mongoose.models.posts || mongoose.model('posts', schema);

What this will do is that it first will check if you have a model called posts that is already compiled and use that. If not it will create and compile it. So the model will only be compiled once and your error should not occur. Hope this helps!

Link to comment
Share on other sites

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...