Mongoose, CastError: Cast to Array failed for value when trying to save a model that contains a model

后端 未结 7 1585
日久生厌
日久生厌 2020-12-09 14:52

I am trying to create the model for my mongodb database using mongoose. This is what I am trying to do:

var Class = mongoose.model(\'Class\', {className: St         


        
7条回答
  •  心在旅途
    2020-12-09 15:42

    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.

提交回复
热议问题