验证来自嵌套模式结构猫鼬的电子邮件 [英] Validate email from a nested schema structure mongoose

查看:111
本文介绍了验证来自嵌套模式结构猫鼬的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to validate email id from nested schema structure from a model but it is showing error:-

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

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.

I want to validate email id for both admin and users .

我该如何纠正?

试图弄清楚我正在做的愚蠢错误,但仍找不到

Trying to figure out what is the silly mistake I am doing but could't find yet

我尝试添加路径adminSchema.admin.path('email').validate((val)我正在获取

I tried to add path adminSchema.admin.path('email').validate((val) I am getting

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

推荐答案

您的adminSchema不包含字段路径email,但是admin.email(或admin.users.$.email作为子模式),猫鼬的确然后,将这些 path 中的任何一个作为Schema实例上的属性.

Yours adminSchema does not contain field path email, but admin.email (or admin.users.$.email as a sub-schema there), Mongoose does have any of those paths as attributes on the instance of the Schema then.

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

So adding the validate middleware is done as:

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

这篇关于验证来自嵌套模式结构猫鼬的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆