Mongoose, CastError: Cast to Array failed for valu

2020-02-05 01:31发布

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: String, marks: [{type: Number}], grades: [{type: Number}]});
var User = mongoose.model('User', {email: String, classes: [Class] });


//Lets create a new user
var class1 = new Class({className: 'aaa', marks: [72, 88, 63], grades: [30, 40, 30]});
var user1 = new User({email: 'aaa@some.com', classes: [class1]});

Saving class1 seems to work okay but when I check mongodb, this is displayed:

{ 
  "_id" : ObjectId("someId"), 
  "className" : "TEST1234", 
  "grades" : [ 30, 40, 30 ], 
  "marks" : [ 72, 88, 63 ], 
  "__v" : 0 
}

What is "__v : 0"?

Saving the user is not successful at all, this is the following error:

ValidationError: CastError: Cast to Array failed for value "{ marks: [ 72, 88, 63 ], grades: [ 30, 40, 30 ], _id: someId, className: 'TEST1234' }" at path "classes" `

What exactly does the error mean? Why is it casting anything to a array? Shouldn't classes: [Class] be an array of type class?

7条回答
仙女界的扛把子
2楼-- · 2020-02-05 01:54

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.

see: https://github.com/Automattic/mongoose/issues/1760

查看更多
ら.Afraid
3楼-- · 2020-02-05 01:38

I got a similar issue using mongoose 5.7.0+ using double nested schema.

Except it wasn't related to the keyword type but a mongoose validation bug.

https://github.com/Automattic/mongoose/issues/8472

Temporary workaround: Use Schema.Types.Mixed for the subschema

查看更多
可以哭但决不认输i
4楼-- · 2020-02-05 01:45

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.

查看更多
Evening l夕情丶
5楼-- · 2020-02-05 01:47

Your model definition is incorrect, you should fix like below.

// var Schema = mongoose.Schema;
var User = mongoose.model('User',{ 
  email: String, 
  classes: [ {type: Schema.Types.ObjectID, ref: 'Class'}] 
});

var Class1 = new Class({/*yourDataWillBeHere*/})

Class1.save(function(err, classData) {
   var User1 = new User({/*YourDataWillBeHere*/})
   User1.classes.push(classData._id);
   User1.save(function(err, userData) {
      //make something with userData object 
   })
})

Then you can get fetched data using with populate() like this

User
.find()
.populate('classes')
.exec()
查看更多
一夜七次
6楼-- · 2020-02-05 01:47

By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a type declaration.

// Mongoose interprets this as 'loc is a String'
var schema = new Schema({ loc: { type: String, coordinates: [Number] } });

Changing the typeKey:

var schema = new Schema({
  // Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates'
  loc: { type: String, coordinates: [Number] },
  // Mongoose interprets this as 'name is a String'
  name: { $type: String }
}, { typeKey: '$type' }); // A '$type' key means this object is a type declaration

Link: http://mongoosejs.com/docs/guide.html#typeKey

查看更多
\"骚年 ilove
7楼-- · 2020-02-05 01:52

Explicitly defining the type rule on a property called type is allowed and won't throw an error. like this:

type: {type: String}
查看更多
登录 后发表回答