node.js - 验证来自嵌套模式结构 Mongoose 的电子邮件

标签 node.js express mongoose mongoose-schema

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

adminSchema.path('email').validate((val) => { 类型错误:无法读取未定义的属性“验证”

模型结构:-

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

最佳答案

您的adminSchema不包含字段路径email,而是admin.email(或admin.users.$.email 作为子模式),Mongoose 确实将这些路径中的任何一个作为模式实例的属性。

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

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

关于node.js - 验证来自嵌套模式结构 Mongoose 的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219664/

相关文章:

node.js - Mongoose 查找给定查询列表中的所有内容

javascript - 获取ajax post请求中传递的参数

javascript - meteor Node :mysql custom query

javascript - `axios.post()` 之后更新状态

node.js - SequelizeEagerLoadingError : model is not associated to otherModel

javascript - 使用 ts-node-dev 运行一个简单的 express 应用程序并得到错误 : False expression: Non-string value passed to `ts.resolveTypeReferenceDirective`

node.js - 使用 Mongoose 并发访问文档

javascript - 通过与 Mongoose 中的不同数组匹配来从嵌入数组中删除元素

javascript - 如何在node.js中获取底层操作系统的临时文件目录路径?

node.js - 使用 Node 和 React 时升级的这个 websocket 是什么?