Closed wzup closed 5 years ago
1.the code dynamically requires any new model as it filter the extensions with .js in the 'models' directory, otherwise you'll have to require them one by one explicitly.
2.Both works, but it's preferred to call the model from mongoose object. In some cases, assume you need to call another file in your model and the file uses the model, you will have circular dependency problem. So this is safer.
Could you please elaborate on that?
require('./userModel')
:// Bootstrap models fs.readdirSync(models) .filter(file => ~file.search(/^[^.].*.js$/)) .forEach(file => require(join(models, file)));
// app/models/user.js
const mongoose = require('mongoose'); mongoose.model('User', UserSchema);