我很頭痛,嘗試通過模塊將一些常見模式定義分享給我的代碼庫中的所有其他模塊。外部模塊中的模式在Node.js中不起作用
我有一個包含這兩種模式一個myproj_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;
而且
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;
導師也是在另一個文件中定義。
我的單元測試這兩個工作在schemas模塊。
當我NPM安裝該模塊並嘗試使用
Entrepreneur = db.model('Entrepreneur', entrepreneurSchema),
我收到以下錯誤創建:
類型錯誤:未定義類型在paths.mentors
你嘗試築巢的架構?您只能使用參考或數組進行嵌套。
如果我在本地模塊中使用相同的代碼,那麼沒問題。 如果我在require中直接引用模式文件(例如require('../ node_modules/myproj_schemas/models/ent_schema'),那麼我會得到錯誤信息。
我確信它不會像這樣破壞較早,但我已經備份了所有的變化,它仍然沒有工作
我畫一個完整的空白,並且任何建議將受到歡迎
編輯:。
我已經創建了一個新的Schemas模塊,它有一個Schema:
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
email: String
});
module.exports = userSchema;
當打包到模塊中並將npm安裝到其他模塊時,這也會失敗。
運行在OS X
感謝這樣一個完整的答案。由於有這個問題,我更多地轉向這種類型的解決方案,它不需要我在模塊之間共享模式。我仍然不清楚爲什麼會出現問題,但我開始認爲這是環境問題,也許一些緩存清理會有所幫助。 – JonRed 2014-11-20 17:37:25
我打算接受這個答案 - 並不是因爲它直接回答我的問題,而是因爲我認爲它將最高級別服務於社區。再次感謝您的努力。 – JonRed 2014-11-20 17:38:16
我沒有真正得到這個工作,並且不得不使用'proxyquire'來覆蓋我的模式定義模塊中使用的Mongoose實例。工程,但似乎很骯髒。 – Mikke 2015-01-28 10:59:01