猫鼬中带有ref的ObjectId数组显示错误将值强制转换为数组失败

时间:2019-03-15 07:11:01

标签: javascript node.js mongodb mongoose

我正在创建带有类别ID数组的博客架构,但是当我尝试在字段中插入数据时会显示

category_id:在路径“ category_id”上由于值[[\'5c8a207e36e3613fe9a6ce30 \',\'5c8a207e36e3613fe9a6ce30 \']“而强制转换为数组

我的模式是

var mongoose = require('mongoose');

var blogSchema = new mongoose.Schema({
    title:{
        type:String,
        required:true
    },
    user_id:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'User'
    },
    category_id:{
        type:[mongoose.Schema.Types.ObjectId],
        ref:'Categories',
    },
    description:{
        type:String,
        required:true
    },
    is_featured:{
        type:Boolean,
        default:false
    },
    isActive:{
        type:Boolean,
        default:true
    },
    created_at:{
        type:Date,
        default:Date.now
    },
    updated_at:{
        type:String
    }
});

var Blog = mongoose.model('Blog',blogSchema);

module.exports.Blog = Blog;

我也尝试

category_id:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Categories',
    }],

但出现相同错误

0 个答案:

没有答案