I created my Schema‘s in my models/mymodel.js file. I also have my models in there as well as my methods.
How do I export them into my routes?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ensure that you set up a bootstrap mechanism that runs all your model files. Once that is done you should have called
mongoose.model("name", Model)for a set of models.This has cached those models in mongoose internally. So you can just call
mongoose.model("name")anywhere you want.The only thing that’s important is order of execution. The model & schemas need to be registered before you run your routes.
This is as a simple as :
Note that normally
readdirSyncis evil but it’s ok to execute blocking calls at startup time (likerequire) before your listen to your server