我有一个使用猫鼬的模块。我的主要项目也使用猫鼬。我发现我无法将模块的连接与项目的架构一起使用。参见示例:
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);
上面的例子抛出
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?
这个问题有什么解决方案?有毛病吗
我发现了the solution
。事实证明,只有在使用本地模块(例如require('../module_with_mongoose_connection')
)时才会引发错误。在上方npm
(内部node_modules
)使用模块时,它可以工作。当猫鼬连接时也会引发错误dynamically patched
(例如,添加诸如的属性mongooseConnection.myattr = 'something'
)。
好吧,这确实很奇怪,但我希望这个答案对其他人有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句