Mongoose ODM model creation problems Mongoose ODM model creation problems mongoose mongoose

Mongoose ODM model creation problems


I would suggest you have a models directory within which you have a file defined for each model. In the model file export the model itself.

In models/Registry.js -

module.exports = mongoose.model('Registry', RegistrySchema);

And in your app.js, just require all the model files -

var fs = require('fs'),    modelPath = __dirname + '/models';fs.readdirSync(modelPath).forEach(function(file) {    require(modelPath + '/' + file);});

Have a look at the MEAN stack boilerplace structure - https://github.com/linnovate/mean/tree/master/app

Also have a look at this article to understand how require works - http://openmymind.net/2012/2/3/Node-Require-and-Exports/