2013-02-14 1 views
0

혼합 컨테이너 내에 저장된 스키마 메소드에 액세스하려고합니다. 상황은 다음과 같습니다. 여러 가지 일 수있는 사례 모델이 있으므로 "caseContent"혼합 속성에 저장된 이러한 각각의 스키마가 있습니다.mongodb가 중첩 컨테이너에서 내 스키마 메소드를 찾지 못합니다.

var CaseSchema = mongoose.Schema({ 
    caseContent : {}, 
    object : {type:String, default : "null"}, 
    collision : {type : Boolean, default : false} 
}); 

caseContent 속성이 다음 exemple이처럼 내 스키마 중 하나의 모델로 가득 :

var TreeSchema = new mongoose.Schema({ 
    appleCount : {type : Number, default : 3} 
}); 
TreeSchema.methods.doStuff = function (data) { 
    console.log('Hey, listen'); 
    return true; 
}; 

그런 다음, 나는 원래의 용기에서 내 스키마의 방법을 사용하려면 :

CaseSchema.methods.doStuff = function (data) { 
    if (this.caseContent.doStuff !== undefined) { 
     this.caseContent.doStuff(); 
        console.log('it worked'); 
    } else { 
     console.log('doStuff is undefined'); 
     console.log(this.caseContent.doStuff); 
    } 
}; 

처음으로 (모든 것이 데이터베이스에 추가 될 때) 작동합니다. 그런 다음 caseContent.doStuff가 항상 정의되지 않은 것 같습니다 (console.log ('doStuff is undefined');가 매번 나타납니다).

그래서 나는 컨테이너의 혼합 된 유형으로 인해 그 방법을 호출하지 못하게 할 것이라고 생각합니다. 거기에는 어떤 작업장이 있습니까?

답변

2

당신은이 스키마 유형 Schema.Types.Mixed

var CaseSchema = mongoose.Schema({ 
    caseContent : Schema.Types.Mixed, 
    object : {type:String, default : "null"}, 
    collision : {type : Boolean, default : false} 
}); 
에게 사용을 시도 할 수 있습니다
관련 문제