2013-02-14 23 views
0

我试图访问存储在混合容器中的模式的方法。这里是的情况: 我有一些案例模型可以是很多不同的东西,所以我有这些东西的模式存储在“caseContent”混合属性中。mongodb在嵌套容器中找不到我的模式方法

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

然后caseContent属性充满了我的模式之一的模型,像这样的一个为例:

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');每次出现)。

所以我认为有些东西让我无法调用该方法,可能是因为容器的混合类型......是否有任何workarround?

回答

2

你可以尝试使用此模式类型Schema.Types.Mixed

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