Wednesday 15 January 2014

mongodb - Schemas in external module not working in Node.js -



mongodb - Schemas in external module not working in Node.js -

i'm having massive headache seek share mutual schema definitions via module other modules in code base.

i have myproj_schemas module contains these 2 schemas:

var mongoose = require('mongoose'), util = require("util"), schema = mongoose.schema; var baseprofileschema = function() { schema.apply(this, arguments); this.add({ _user: {type: schema.types.objectid, ref: 'user', required: true}, name: {type: string, required: true}, bio: {type: string, required: true}, picturelink: string }); }; util.inherits(baseprofileschema, schema); module.exports = baseprofileschema;

and

var mongoose = require('mongoose'), baseprofileschema = require('./base_profile_schema.js'), schema = mongoose.schema; var entschemaadditions = { mentors: {type: schema.types.objectid, ref: 'mentor'} }; var entrepreneurschema = new baseprofileschema(entschemaadditions); module.exports = entrepreneurschema;

mentors defined in file.

my unit tests these both work in schemas module.

when npm install module , seek create using

entrepreneur = db.model('entrepreneur', entrepreneurschema),

i next error:

typeerror: undefined type @ paths.mentors did seek nesting schemas? can nest using refs or arrays.

if utilize same code in local module, no problem. if reference schema file straight in require (e.g. require('../node_modules/myproj_schemas/models/ent_schema') error.

i'm sure wasn't breaking earlier, i've backed out changes , still not working.

i'm drawing finish blank, , suggestions gratefully received.

edit:

i've created new schemas module. has 1 schema:

var mongoose = require('mongoose'); var userschema = new mongoose.schema({ email: string }); module.exports = userschema;

this fails when packaged in module , npm install'd other modules.

running on os x

personally created separate "common" project init method register of models mongodb, , phone call init method in app.js file of apps need access models.

create shared project - create new node project, next standard process.

package.json - in shared project, set package.json file contain next entry:

"main": "index.js"

add model - create new models folder within shared project, contain of mongoose schemas , plugins. add together userschema file models folder , name user.js.

var mongoose = require('mongoose'); var userschema = new mongoose.schema({ email: string }); module.exports = mongoose.model('user', userschema);

index.js - in project's root index.js file create shared object can used apps, exposing models , init method. there's many ways code this, here's how i'm doing it:

function common() { //empty array hold mongoose schemas this.models = {}; } common.prototype.init = function(mongoose) { mongoose.connect('your mongodb connection string goes here'); require('./models/user'); //add more model references here //this create referencing models easier within apps, don't have utilize strings. model name utilize here must match name used in schema file this.models = { user: mongoose.model('user') } } var mutual = new common(); module.exports = common; reference common project - want reference shared project, add together reference shared project in package.json file within app , give name of common. used github store project , referenced repository path. since repository private had utilize key in path, covered on github back upwards site.

init models in app - in start script app (let's assume it's app.js example) add together reference common project , phone call init method connect mongodb server , register models.

//at top, near other module dependencies var mongoose = require('mongoose') , mutual = require('common'); common.init(mongoose);

use model anywhere in app - mongoose has connection pool established , models have been registered, can utilize models of classes within app. example, have page displays info user (untested code, wrote example):

var mutual = require('common'); app.get('/user-profile/:id', function(req, res) { common.models.user.findbyid(req.params.id, function(err, user) { if (err) console.log(err.message); //do else handle error else res.render('user-profile', {model: {user: user}}); }); });

edit sorry, didn't see line inheriting 1 schema another. 1 of other answers stated, mongoose offers concept of plugin. in illustration above, this:

in mutual module, under '/models/base-profile-plugin.js'

module.exports = exports = function baseprofileplugin(schema, options){ //these paths added schema uses plugin schema.add({ _user: {type: schema.types.objectid, ref: 'user', required: true}, name: {type: string, required: true}, bio: {type: string, required: true}, picturelink: string }); //you can add together static or instance methods or shared getter/setter handling logic here. see plugin documentation on mongoose website. }

in mutual module, under '/models/entrepreneur.js

var mongoose = require('mongoose') , baseplugin = require('./base-profile-plugin.js'); var entrepreneurschema = new mongoose.schema({ mentors: {type: schema.types.objectid, ref: 'mentor'} }); entrepreneurschema.plugin(baseplugin); module.exports = mongoose.model('entrepreneur', entrepreneurschema);

node.js mongodb mongoose

No comments:

Post a Comment