I am trying to access the method of a schema which is stored inside a mixed container. Here is the situation : I have some cases model which can be many different things, so I have a schema for each of these things which are stored in the "caseContent" mixed property.
var CaseSchema = mongoose.Schema({
caseContent : {},
object : {type:String, default : "null"},
collision : {type : Boolean, default : false}
});
The caseContent property is then filled with the model of one of my schemas, like this one for exemple :
var TreeSchema = new mongoose.Schema({
appleCount : {type : Number, default : 3}
});
TreeSchema.methods.doStuff = function (data) {
console.log('Hey, listen');
return true;
};
Then, I want to use the method of my schema from the original container :
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);
}
};
On the first time (when everything is added on the database) it works. Then, the caseContent.doStuff seems to be always undefined (the console.log('doStuff is undefined'); appears each time).
So I think there is something that keeps me from calling that method probably because of the mixed type of the container... Is there any workarround for that ?
You could try to use this schema type Schema.Types.Mixed
var CaseSchema = mongoose.Schema({
caseContent : Schema.Types.Mixed,
object : {type:String, default : "null"},
collision : {type : Boolean, default : false}
});