猫鼬传递空字符串

Mongoose passing empty string

你好,我有一个模式,当我传递值时,它显示值是空的并且是必需的。我不明白为什么。

Model.js:-

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:[]    
            }                       
});



// Adding custom validation for email.
adminSchema.path('admins.email').validate((val) => {
    emailRegexx = /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ;
    return emailRegexx.test(val);   
}, 'Invalid email');


//Pre event to trigger saltSecret before save function in user controller
adminSchema.pre('save', function (next){
    bcryptt.genSalt(10, (err, salt) => {            
        bcryptt.hash(this.admin.admins.password, salt, (err, hash) => { 
            this.admin.admins.password = hash ;
            this.admin.admins.saltSecret = salt;
            next();
        });
    });
});

mongoose.model('Admin', adminSchema); 

控制器文件:-

const mongoose = require ('mongoose');
const Admin = mongoose.model('Admin');
var MongoClient = require('mongodb').MongoClient;

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",
                  id : req.body._id 
    }; 

const secret = crypto.randomBytes(20);
const hash = crypto.createHmac('sha256', secret)
                   .update(secret + admin.admins.email)
                   .digest('hex');

const reqq = crypto.createHash('md5').update(admin.companyName).digest('hex');

let valueNum = reqq.match(/\d/g).join("").toString().substring(0,6);

admin.companyID = valueNum;

var expires = new Date();
expires.setHours(expires.getHours() + 6);

    admin.admins.emailResetTokenn = hash;
    admin.admins.emailExpires = expires;    
    admin.save((err, doc) =>{});

我知道我犯了一些愚蠢的错误,但没有弄清楚我到底错过了什么。

测试时我通过了 :-

{
    "companyName":"Microsoft",
    "address": "AUS",
    "contactDetails" : "54534454",
    "admins" : {
        "email" : "kapa@clickmail.info",
        "password" : "something#1",
        "firstName" : "hdsdsds",
        "lastName": "Ghodsdsdsh",
        "phoneNumber" : "4544343",
        "designation" : "Software Engineer"
    }
}

输出为:-

[
    "Email can't be empty.",
    "Password name can't be empty.",
    "First name can't be empty.",
    "Last name can't be empty.",
    "Reqired for further contact. Can't be empty.",
    "Designation can't be empty."
]

我是否以错误的方式定义了模式结构?

如果有一点建议,我们将不胜感激。谢谢。

编辑:- 预期 return

{
    "companyName":"Microsoft",
    "companyId" : "4554"
    "address": "AUS",
    "contactDetails" : "54534454",
    "admins": {
        "email" : "bayicucasu@first-mail.info",
        "password" : "dffooto465m56.545mom3.4$%$%brgg",
        "firstName" : "hdsdsds",
        "lastName": "Ghodsdsdsh",
        "phoneNumber" : "4544343",
        "designation" : "Software Engineer",
        "id" : "565645vfbtrbfcv678",
        "verified": false,
        "role" : "admin",
        "emailResetTokenn" : "45455454rbgtbg$$t4@67&jyynjyj",
        "emailExpires": "21-05-2016 17:00:00",
        "saltSecret": "dfjduh%ufheHUHF7.dfu%#jj",
        "users":[]        
}
}

在您的代码中,例如,email 字段,您可以使用:

email : req.body.email

表示email必须在请求体的最顶层

尝试这样的事情:

{
    "companyName":"Microsoft",
    "address": "AUS",
    "contactDetails" : "54534454",
    "email" : "kapa@clickmail.info",
    "password" : "something#1",
    "firstName" : "hdsdsds",
    "lastName": "Ghodsdsdsh",
    "phoneNumber" : "4544343",
    "designation" : "Software Engineer"
}

如果你想保留请求主体,我认为你必须像这样更改代码:

admin.admins = {
                  email : req.body.admins.email,
                  password: req.body.admins.password,
                  firstName : req.body.admins.firstName, 
                  lastName : req.body.admins.lastName,
                  phoneNumber : req.body.admins.phoneNumber,
                  designation : req.body.admins.designation,
                  role : "admin",
                  id : req.body.admins._id 
    }; 

类似的东西,还要注意请求正文中没有_id字段

如果您不想在请求正文中指定_id,您可以使用

生成它
id: mongoose.Types.ObjectId()

或您喜欢的任何随机方法