2013-02-06 3 views
5

두 개의 스키마, TeamMatch이 있습니다. Match Schema에 팀을 식별하려면 Team Schema을 사용하고 싶습니다. 지금까지, 여기 내 팀 및 매치 JS 파일이 있습니다. 팀 스키마를 내 경기 스키마에 연결하여 집 또는 부재 중 팀을 간단히 식별 할 수 있도록하고 실제 경기도 개체에 일치 스키마를 저장하려고합니다.2 개의 몽구스 스키마 연결하기

여기

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.Teams.home.name = England 예를 들어 홈 팀을 참조 할 수 있습니다이 방법은 내가 함께 할 것을 시도하고있는 무슨이다 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); 

답변

2

귀하의 솔루션 :

: 오히려 스키마를 사용하는 모델보다 실제 스키마를 사용하여
module.exports = mongoose.model('Team', Team); 

대신 당신은 둥지 스키마 싶지 않아 직접

+0

내가 말한대로 팀 내보내기를 변경했습니다. Match.js 파일에서 나는'var definition = require ('Team.js');를 가지고 있고'hometeam'을'definition.schema'로 저장하고 있습니다 만''Team Team.js 모듈을 찾을 수 없습니다. , 어떤 생각? – germainelol

+0

필자도 Team Team.js를 넣지 않으려 고 시도했습니다 – germainelol

+0

다시 말씀 드려서 죄송 합니다만 전체 경로를 지정하여이 문제를 해결했습니다. 이제'TypeError :'hometeam'에 정의되지 않은 유형이 표시되고 스키마가 중첩되어 있는지 묻습니다. – germainelol

1

모델의 definition.schema을 사용하고

module.exports = { 
    model: mongoose.model('Team', Team), 
    schema: Team 
}; 

다음 var definition = require('path/to/js');에.

몽구스 인구 : http://mongoosejs.com/docs/populate.html 귀하의 문제를 해결할 수 있습니다.