I'm having a massive headache try to share some common schema definitions via a module to all the other modules in my code base.
I have a myproj_schemas module that contains these two 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 is also defined in another file.
My unit tests for these both work in the schemas module.
When I npm install this module and try to create using
Entrepreneur = db.model('Entrepreneur', entrepreneurSchema),
I get the following error:
TypeError: Undefined type at paths.mentors
Did you try nesting Schemas? You can only nest using refs or arrays.
If I use the same code in my local module, then no problem. If I reference the schema file directly in the require (e.g. require('../node_modules/myproj_schemas/models/ent_schema') then I get the error.
I'm fairly sure it wasn't breaking like this earlier, but I've backed out all the changes and it is still not working.
I'm drawing a complete blank, and any suggestions would be gratefully received.
EDIT:
I've created a new Schemas module. It has one Schema:
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
email: String
});
module.exports = userSchema;
This also fails when packaged up in a module and npm install'd to other modules.
Running on OS X