i'm trying to keep my models separate, but it's telling me a model
isn't registered:
## console
/home/swilson/node/fr/node_modules/mongoose/lib/index.js:222
throw new Error('Schema hasn\'t been registered for model "' + name + '"
^
Error: Schema hasn't been registered for model "Org".
Use mongoose.model(name, schema)
at Mongoose.model
(/home/swilson/node/fr/node_modules/mongoose/lib/index.js:222:13)
at new fr_org.init (/home/swilson/node/fr/model/fr/org.js:7:19)
at Object.fr_main.init (/home/swilson/node/fr/model/fr/main.js:9:15)
at new fr.init (/home/swilson/node/fr/model/fr.js:19:22)
at bootPart (/home/swilson/node/fr/app.js:99:59)
at /home/swilson/node/fr/app.js:88:13
at Array.forEach (native)
at /home/swilson/node/fr/app.js:86:11
at Object.oncomplete (fs.js:308:15)
at process.startup.processMakeCallback.process._makeCallback
(node.js:248:20)
// fr.js model
var mongoose = require('mongoose'),
FR = require('./fr/main.js');
var fr = {
module: 'mongoose',
init: function (app) {
var log = app.settings.log;
var conn = app.settings.nconf.get("db:fr");
mongoose.connect(conn.string);
mongoose.connection.on('open', function() {
log.log("%s Connected to %s: %s ",
conn.name, conn.type, conn.string);
});
this.conn = conn;
this.db = mongoose;
this.fr = FR.init(this.db); // return model
return this;
},
find: function (parms, cb) {
......
}
};
module.exports = fr;
// fr/main.js schema
var org = require('./org.js');
var dates = require('./dates.js');
var fr_main = {
init: function (db) {
var schema = db.Schema;
Org = new org.init(db);
Dates = new dates.init(db);
return db.model('FR', this.mainSchema);
},
mainSchema: function () {
var main = new this.schema({
..........
org: [Org],
dates: [Dates],
});
return this.main;
},
};
module.exports = fr_main;
// fr/org.js schema
var fr_org = {
init: function (db) {
var schema = db.Schema;
return db.model('Org', this.orgSchema);
},
orgSchema: function () {
var org = new this.schema({
.........
});
return
this.org;
},
};
module.exports = fr_org;
## and, if it matters, the pertinent parts of app.js
boot(app, 'model');
.......
function boot (app, part) {
fs.readdir(path + '/' + part, function(err, files){
if (err) throw err;
files.forEach(function(file){
if (file.match(/.*\.js$/)) {
bootPart(app, part, file);
}
});
});
};
function bootPart (app, part, file) {
// container_name => object
// ie, v_index
var cname = part.substr(0,1);
var obj = require(path + '/' + part + '/'+ file);
app.settings[cname + '_' + file.replace('.js', '')] = new obj.init(app);
}
the goal would be able to call functions defined in model/fr.js from
controllers by calling
app.settings.fr[fnName](parms) and not need to
care about what modules were required to get that data (ie, i don't
want to require mongoose or types/validate everywhere)