缺少一个架构值

时间:2019-05-21 12:16:24

标签: node.js express mongoose mongoose-schema

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

所有细节都显示在回调json上,除了一个即; 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
}

为什么会错过它?

1 个答案:

答案 0 :(得分:0)

我实际上不确定为什么未设置该值,但是如果您没有创建管理文档,然后在保存之前对其进行了编辑,但是您创建了管理对象,然后将其传递给新的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();
}