我有兩個模式,一個Team
和一個Match
。我想使用Team Schema
來確定Match Schema
中的團隊。到目前爲止,這裏是我的團隊和匹配JS文件。我想將Team Schema鏈接到我的匹配架構,以便我可以簡單地識別主隊或客隊,並且我將匹配架構存儲爲一個實際的Team對象。鏈接2貓鼬模式
這樣我可以參考主隊例如爲Match.Teams.home.name = England
(這只是過程的示例)
Team.js
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validatePresenceOf = function(value){
return value && value.length;
};
var getId = function(){
return new Date().getTime();
};
/**
* The Team schema. we will use timestamp as the unique key for each team
*/
var Team = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'name' : { type : String,
validate : [validatePresenceOf, 'Team name is required'],
index : { unique : true }
}
});
module.exports = mongoose.model('Team', Team);
這裏就是我試圖做Match.js
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeamSchema = require('mongoose').model('Team');
var validatePresenceOf = function(value){
return value && value.length;
};
var toLower = function(string){
return string.toLowerCase();
};
var getId = function(){
return new Date().getTime();
};
/**
* The Match schema. Use timestamp as the unique key for each Match
*/
var Match = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'hometeam' : TeamSchema,
'awayteam' : TeamSchema
});
module.exports = mongoose.model('Match', Match);
我按照你的說法改變了團隊出口。在Match.js文件中我現在有'var definition = require('Team.js');'並且我將'hometeam'存儲爲'definition.schema',但是我得到'找不到模塊Team.js'的錯誤, 任何想法? – germainelol
我也嘗試過只是把Team not Team.js – germainelol
抱歉再次發表評論,但我通過放置完整路徑來解決這個問題,現在在'hometeam'上得到'TypeError:Undefined type並詢問我是否嵌套模式 – germainelol