【问题标题】:One schema value is missing out缺少一个架构值
【发布时间】:2019-05-21 20:38:42
【问题描述】:

我正在注册公司详细信息,并尝试验证该公司管理员的 电子邮件 ID 是否为 true

所有细节都显示在回调 json 上,除了一个 ie; verified

这是我的架构详细信息:-

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.",
                },
    admins:     {
                        email :     {
                                    type: String,
                                    required: "Email can't be empty.",
                                    unique: true
                                    },
                        password:   {
                                    type: String,
                                    required: "Password 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:[]    
    }           
});

在控制器中我正在做:-

module.exports.registerAdmin = (req, res, next) =>{ 

    var admin = new Admin();
    admin.companyName = req.body.companyName;
    admin.address = req.body.address;
    admin.contactDetails  = req.body.contactDetails;
    admin.admins = {
                  email : req.body.email,
                  password: req.body.password, 
                  firstName : req.body.firstName, 
                  lastName : req.body.lastName,
                  phoneNumber : req.body.phoneNumber,
                  designation : req.body.designation,
                  role : "admin",
                  users: []
    };

当我运行路由 api 时,verified 详细信息未显示为 verified : false

结果:-

{
    "admins": {
        "email": "losefivex@mrmail.info",
        "password": "$2a$10$pgkFZAfGwHkJ2e88/d2gUeWtjVdinxGmYGx5Euh69kgH95nCIUcNa",
        "firstName": "hdsdsds",
        "lastName": "Ghodsdsdsh",
        "phoneNumber": "4544343",
        "designation": "Software Engineer",
        "role": "admin",
        "users": [],
        "emailResetTokenn": "c833599ab72255a957007b42ca1cb8fddd566d7474b8b2b92bc08252f60184fa",
        "emailExpires": "2019-05-21T18:05:46.095Z",
        "saltSecret": "$2a$10$pgkFZAfGwHkJ2e88/d2gUe"
    },
    "_id": "5ce3e99a896e8c3ff5665702",
    "companyName": "Meta",
    "address": "AUS",
    "contactDetails": "54534454",
    "companyID": "675521",
    "__v": 0
}

为什么会丢失?

【问题讨论】:

    标签: node.js express mongoose mongoose-schema


    【解决方案1】:

    我实际上不太确定为什么没有设置该值,但是如果您不创建管理文档然后在保存之前对其进行编辑,而是创建管理对象并将其传递给新的 Admin() 构造函数之后,正在设置默认值:

    const mongoose = require('mongoose');
    
    const STACKOVERFLOW_ISSUE = `56238095`;
    const connectionString = `mongodb://localhost:27017/${ STACKOVERFLOW_ISSUE }`;
    const { Schema, SchemaTypes } = mongoose;
    
    run()
        .then(() => console.log('done'))
        .catch(error => console.error(error.stack));
    
    async function run() {
        await mongoose.connect(connectionString);
        await mongoose.connection.dropDatabase();
    
    
    
        const AdminSchema = new Schema({
            test: String,
            admins: {
                email: String,
                verified: {
                    type: Boolean,
                    default: false,
                }
            }
        });
    
        const Admin = mongoose.model('admins', AdminSchema);
    
        /* THE INTERESTING PART BELOW*/
        const newAdmin = {
          test: 'someString',
          admins: {
            email: 'someEmail'
          }
        }
        // creating testObject by passing the blueprint to it
        const test1 = new Admin(newAdmin);
        await test1.save();
    
        process.exit();
    }
    

    【讨论】:

    • 感谢您的解释,但我没有在保存前编辑值。我在其他架构上做同样的事情,但不知道这里发生了什么
    • 当我说“编辑”时,我的意思是你首先调用构造函数,然后设置属性,而不是先创建一个属性对象,然后用它调用构造函数。我的建议为什么它在其他地方工作是因为它不是嵌套值?由于不需要管理员,因此它可能为空,因此 mongoose 不会在创建时设置默认值。您也可以尝试使用 .markModified() 查看是否可以找到解决方案
    • 是的,你是对的。它不是其他模型上的嵌套值。谢谢你的解释。我真的需要这个解决方案才能工作,因为它取决于我的登录系统。那么我必须在我的控制器中进行哪些更改才能需要admins
    • 您可以尝试将 admins.admins 的当前架构定义包装在 .type 定义中,这样您也可以将其设置为 required:true。这可能有效:admin.admins: {type: {email: req.body.email ...}, required: true}。不过,是什么阻止您快速重构代码以使用“蓝图”对象调用构造函数?
    猜你喜欢
    • 2012-08-21
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多