从嵌套模式结构猫鼬验证电子邮件

Validate email from a nested schema structure mongoose

我正在尝试从嵌套模式结构中验证电子邮件 ID 模型,但显示 error:-

adminSchema.path('email').validate((val) => { TypeError: Cannot read property 'validate' of undefined

模型结构:-

var adminSchema = new mongoose.Schema({
    companyName : {
                type: String,
                required: "Company  name can't be empty.",
                required: false
                },  
    companyID:  {
                type: String,
                },              
    address :   {
                type: String,
                required: "Address can't be empty.",
                },
    contactDetails : {
                type: String,
                required: "Company contact number can't be empty.",
                },
    admin: {
                        email :     {
                                    type: String,
                                    required: "Email can't be empty.",
                                    unique: true
                                    },
                        password:   {
                                    type: String,
                                    required: "First name can't be empty."
                                    },
                        firstName : {
                                    type: String,
                                    required: "First name can't be empty."
                                    },
                        lastName : {
                                    type: String,
                                    required: "Last name can't be empty."
                                    },  
                        phoneNumber :   {
                                    type: String,
                                    required: "Reqired for further contact. Can't be empty."
                                    },
                        designation :   {
                                    type: String,
                                    required: "Designation can't be empty."
                                    },          
                        verified: { 
                                    type: Boolean, 
                                    default: false 
                                    },
                        role: String,
                        emailResetTokenn: String,
                        emailExpires: Date,
                        saltSecret: String,//this is user for encryption and decryption of password 
                        users:[{
                                email :     {
                                            type: String,
                                            required: "Email can't be empty.",
                                            unique: true
                                            },
                                password:   {
                                            type: String,
                                            required: "First name can't be empty."
                                            },
                                firstName : {
                                            type: String,
                                            required: "First name can't be empty."
                                            },
                                lastName : {
                                            type: String,
                                            required: "Last name can't be empty."
                                            },  
                                phoneNumber :   {
                                            type: String,
                                            required: "Reqired for further contact. Can't be empty."
                                            },          
                                verified: { 
                                            type: Boolean, 
                                            default: false 
                                            },
                                role: String,
                                emailResetToken: String,
                                emailExpires: Date,
                                saltSecret: String //this is user for encryption and decryption of password
                        }]  
            }                       
});

我想验证管理员和用户的电子邮件 ID。

我怎样才能让它更正?

试图弄清楚我正在犯的愚蠢错误是什么,但还没有找到

我尝试添加路径 adminSchema.admin.path('email').validate((val) 我得到

adminSchema.admin.path('email').validate((val) => {
                  ^
TypeError: Cannot read property 'path' of undefined

将 admin 的所有模式放入数组中。我不确定,但请尝试一次

你的 adminSchema 不包含字段路径 email,但 admin.email(或 admin.users.$.email 作为 sub-schema),Mongoose 确实有任何那些 paths 作为 Schema 实例的属性。

所以添加 validate 中间件是这样完成的:

adminSchema.path('admin.email').validate(...)