Reputation: 180
I am creating node app using mongo mongoose .I have models separate file where all data modules are created and i want to be configured all data models into one js files which we have to include in node.js file(server.js) Here i have user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = function() {
var users = new Schema({
name : String
, body : String
, date : Date
});
mongoose.model("users", users);
};
books.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = function() {
var books = new Schema({
title : String
});
mongoose.model("books", books);
};
now i want to be include these books.js and user.js file into one single js file so i created one
model.js
var models = ['users.js','books.js'];
module.exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
require(models[i])();
}
};
and i include this model.js file in server.js file
require('./models/model.js').initialize();
When i was trying to run this ,i got error that " cannot find model books.js and user.js"
i think the error is in model.js So can you please correct me.
Upvotes: 2
Views: 1285
Reputation: 5488
You could change your models.js
file to something like this:
var users = require('./users');
var books = require('./books');
module.exports.initialize = function () {
return {
users: users(),
books: books()
};
}
Make sure your require()
s are accessing the correct file paths.
Upvotes: 1