你好,我有一个架构,当我传递值时,它表明值是空的并且是必需的。我不明白为什么。
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."
]
我是否以错误的方式定义了架构结构?
一点建议将不胜感激。谢谢。
编辑:-预期收益
{
"companyName":"Microsoft",
"companyId" : "4554"
"address": "AUS",
"contactDetails" : "54534454",
"admins": {
"email" : "bayicucasu@first-mail.info",
"password" : "dffooto465m56.545mom3$5$6.4$%$%$64brgg",
"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":[]
}
}
答案 0 :(得分:0)
在您的代码中,例如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()
或您喜欢的任何随机方法