separate models

86 views
Skip to first unread message

shawn wilson

unread,
Sep 16, 2012, 7:43:05 AM9/16/12
to mongoo...@googlegroups.com
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)

shawn wilson

unread,
Sep 17, 2012, 1:50:00 PM9/17/12
to mongoo...@googlegroups.com
so, i found this:

which seems like a good way to separate models (and i'll probably go with this pattern). however, i still don't understand why what i did is failing?

shawn wilson

unread,
Sep 18, 2012, 4:25:48 AM9/18/12
to mongoo...@googlegroups.com
so, this is just weird... i pretty much implemented what the link i posted explained and the app loads and ~works. the part that doesn't seem to work is the actual db process - no data gets inserted, and i don't know where the results i'm getting come from.

first, this should mean there's nothing in the db, right?
 % mongoexport --collection fr                                                  
connected to: 127.0.0.1
exported 0 records

however, when i query the app, i get results timestamped from previous posts (no data other than the timestamp, but):
  % curl http://localhost:3000/fr | head -19                                               ~ graytop
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   969  100   969    0     0  43529      0 --:--:-- --:--:-- --:--:-- 46142
[
  {
    "_id": "50581daf18aa83736f000001",
    "__v": 0,
    "dates": [],
    "org": [],
    "data": {
      "stamp": "2012-09-18T07:07:27.175Z"
    }
  },
  {
    "_id": "5058205c1d896e8714000003",
    "__v": 0,
    "dates": [],
    "org": [],
    "data": {
      "stamp": "2012-09-18T07:18:52.729Z"
    }
  },


so, i'm guessing i'm not looking at something right. but, more weirdness, if i data.push({"some":"data"}) in the controller under app.get, it does what it's supposed to and inserts data. however, if i do the same thing before the cb in my model, nothing.

here's the code i'm using now (app.js is intact):

 % cat controller/data.js                                                       
 node/fr (master \342\232\241) graytop
// data.js controller

var data = {
    init: function (app) {
        var log = app.settings.log;
        var fr = app.settings.m_fr;
        log.log("Loading data.js controller");

        app.get('/fr', function (req, res, next) {
console.log("controller get");
            fr.find({}, function (err, data) {
data.push({"testing":"this"});
// ^^ works as intended
                res.send(data);
            } );
        } );

        app.post('/fr', function (req, res, next) {
            fr.create(req.body, function (err, data) {
                if (err) {
                    console.log(err);
                    res.send("FAIL");
                } else {
                    res.send(JSON.stringify(data));
                }
           } );
        } );
    },
};

module.exports = data;


// fr.js model

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
    FR = require('./fr/main.js').main(Schema, mongoose);

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);
        });
        return mongoose.model('Main');
    },
    find: function (parms, cb) {
console.log("find always");
// i never see this message
......
    create: function (input, cb) {
console.log("create always");
// never see this message either
        if ('undefined' === typeof(input)) {
            cb("No input", null);
        }


// fr/main.js schema

function main (Schema, mongoose) {
    var Org = require('./org.js').org(Schema, mongoose);
    var Dates = require('./dates.js').dates(Schema, mongoose);

    var mainSchema = new Schema({
        data: {
...........
        },
        org: [Org],
        dates: [Dates],
    });
    mongoose.model('Main', mainSchema);
}

module.exports.main = main;



any help here would be great. i feel like i'm banging my head against a wall here.
Reply all
Reply to author
Forward
0 new messages