Mongoose model Schema with reference array: CastError: Cast to ObjectId failed for value “[object Object]”

前端 未结 2 735
甜味超标
甜味超标 2021-02-08 04:34

I build a blog website with express.js and mongoosejs. A article may have one or more category. When I create a new article, I get error:

{ [CastError: Cast to O         


        
2条回答
  •  渐次进展
    2021-02-08 05:13

    Please use mongoose.Schema.Types.Mixed as data type of categories. I had the same issue with saving data array. It works for me.

    var mongoose = require('mongoose'),
        Schema = mongoose.Schema;
    
    var ArticleSchema = new Schema({
        created: { type: Date, default: Date.now },
        title: String,
        content: String,
        summary: String,
        categories: [{type: Schema.Types.Mixed }]
    });
    
    mongoose.model('Article', ArticleSchema);
    

提交回复
热议问题