Sunday, 19 March 2017

mongoose schema hasn't registered for model

My project has such structure

|-index.js |--models/ |---post.model.js |---user.model.js |-modules/ |--auth/ |---auth.controller.js |--post |---post.controller.js

In index.js,I require every model from models/, and I can use mongoose.model('post') to get post model.

But in other module, such in auth.controller.js, I can't simply use mongoose.model('post') to get post model, i must require post.model.js first. Otherwise there will be error Schema hasn't been registered for model 'post'.

So is there anyway to avoid require any model.js file, just use require('mongoose') and mongoose.model to get the modle I want outside the index.js?

In MEAN.js, i can add my own module and don't need to require model file in controller to get the model, but i can't figure out how does it work?

So anyone can help?



via halsn

No comments:

Post a Comment