I have a module that uses mongoose. My main project also uses mongoose. I found out that I can not use module's connection with my project's schema. See the example:
var mod = require('module_with_mongoose_connection');
var mongoose = require('mongoose');
var SessionSchema = new mongoose.Schema({ // replacing this with `mod.mongoose.Schema` works
...
});
mod.mongooseConnection.model('session', SessionSchema);
The example above throws
throw new TypeError('Undefined type at `' + path +
^
TypeError: Undefined type at `paths.name`
Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (/Users/me/Work/me/nodejs/orm-model/node_modules/mongoose/lib/schema.js:397:11)
at Schema.path (/Users/me/Work/me/nodejs/orm-model/node_modules/mongoose/lib/schema.js:334:29)
at Schema.add (/Users/me/Work/me/nodejs/orm-model/node_modules/mongoose/lib/schema.js:245:12)
at Schema.add (/Users/me/Work/me/nodejs/orm-model/node_modules/mongoose/lib/schema.js:240:14)
at new Schema (/Users/me/Work/me/nodejs/orm-model/node_modules/mongoose/lib/schema.js:72:10)
Why is that? A bug?
What's the solution to this problem? A bug?
I found the solution
. It turns out that the error is thrown only when using local modules (e.g. require('../module_with_mongoose_connection')
). When using a module over npm
(inside node_modules
) it works. The error is also thrown when mongoose connection is dynamically patched
(e.g. you add an attribute like mongooseConnection.myattr = 'something'
).
Well this is really weird but I hope this answer will help others.
I had the same error, my problem was that I had a spelling mistake in my require statement:
var mongoose = require('Mongoose'); // Don't do this
var mongoose = require('mongoose'); // Use a lowercase 'm'