Mongoose, CastError: Cast to Array failed for value when trying to save a model that contains a model
Man, I had a similar issue creating an Schema like this:
QuestionnaireSchema = mongoose.Schema({ formId: Number, name: String, questions: [ { type: String, title: String, alternatives:[{ label: String, value: "Mixed" }] } ]});
My mistake was that I am using "type" as a field name and this is reserved word in mongoose.
I just change:
type: String,
to
formType: String,
and that works.
Try changing the class definition to :
var classSchema = mongoose.Schema({className: String, marks: [{type: Number}], grades: [{type: Number}]});var userSchema = mongoose.Schema({email: String, classes: [classSchema] });var User = mongoose.model('User',userSchema);
This is required since mongoose is not able to parse the object without a related schema. Now when you create a new Schema for the internal class object and refer it in the main userSchema mongoose should be able to parse your object.