猫鼬自定义验证课程的先决条件?

我有以下CourseSchema,

我需要通过以下条件来验证先决条件字段:

-它必须为空,

-或长度为3,并且与现有课程ID匹配

我该如何处理?我需要在这里连接到MongoDB吗? 预先感谢!

let courseSchema = new mongoose.Schema({
    courseID: {
        type: String,minlength: 3,maxlength: 3,required: [true,"missing courseID"],unique: true
    },courseName: {type: String,"missing courseName"]},prerequisites: {
        type: String,validate:{
            validator: function(v){
                if(v.length === 0){
                    return true;
                }
                else if(v.length === 3){
                    //check if the prerequisite course exist in the DB
                        //if not return false
                        //else return true
                }
            },message: "prerequisites must either be empty or (have length= 3 and exist)"
        }
    }
})
lizihao3hao 回答:猫鼬自定义验证课程的先决条件?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3164685.html

大家都在问