验证来自嵌套模式结构猫鼬的电子邮件

时间:2019-05-20 11:04:46

标签: node.js express mongoose mongoose-schema

我正在尝试从一个嵌套的架构结构中验证电子邮件ID 模型,但显示错误:-

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

2 个答案:

答案 0 :(得分:1)

您的adminSchema不包含字段路径email,但包含admin.email(或admin.users.$.email作为子模式),猫鼬的确具有以下任何一个路径作为架构实例的属性。

因此,添加validate中间件的操作如下:

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

答案 1 :(得分:0)

将所有admin架构放入数组。我不确定,请尝试一次