首页 > 解决方案 > 验证参考字段是否为空不在月光中工作

问题描述

我的问题是 SchemaA 中的选项字段没有验证它是否为空,即使放置了所需的属性。

这是我的模型的一个例子

const schemaA = new Schema({
       title:{
           type: String,
           required: [true,'this field is required']
       },
       options: [{
        type: Schema.Types.ObjectId,
        ref: 'SchemaB',
        default: undefined,
        required: [true, "cant be empty"]
       }]
})


const schemaB = new Schema({
   title:{
       type: String,
       required: [true,'this field is required']
   }
})

标签: mongodbmongoose

解决方案


我将 schemaA 选项字段更改为此。

const schemaA = new Schema({
       title:{
           type: String,
           required: [true,'this field is required']
       },
       options: {
        type: [Schema.Types.ObjectId],
        ref: 'SchemaB',
        default: undefined,
        required: [true, "cant be empty"]
       }
})

推荐阅读