gpt4 book ai didi

node.js - Mongoose 中使用错误验证器的路由

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:13 25 4
gpt4 key购买 nike

我正在使用 Postman 来测试路由,该路由在 user.save((err)=>{}) 后返回错误消息,表示密码太长。它使用我创建的 passwordValidator,但显然我没有在 Schema 中为该路由调用它。

我做错了什么?

Mongoose 中的用户模式:

    const userSchema=new Schema({
email: { type: String, required: true, unique: true, lowercase: true, validate: emailValidators},
username: { type: String, required: true, unique: true, lowercase: true, validate: usernameValidators},
bio: { type:String,default:null,validate:bioValidators},
location: {type:String, default:null},
gender: {type:String,default:null,validate:genderValidators},
birthday: { type:String,default:null},
password: { type: String, required: true,validate: passwordValidators}
});

路线:

router.put('/editProfile',(req,res)=>{
if(!req.body.bio){
res.json({success:false,message:"No bio provided"});
}
else{
if(!req.body.location){
res.json({success:false,message:"No location provided"});
}
else{
if(!req.body.gender){
res.json({success:false,message:"No gender provided"});
}
else{
if (!req.body.birthday) {
res.json({success:false,message:"No birthday provided"});
}
else{
User.findOne({_id:req.decoded.userId},(err,user)=>{
if(err){
res.json({success:false,message:"Something went wrong: "+err});
}
else{
if(!user){
res.json({success:false,message:"User not found"});
}
else{
user.bio=req.body.bio;
user.location=req.body.location;
user.gender=req.body.gender;
user.birthday=req.body.birthday;
user.save((err)=>{
if(err){
res.json({success:false,message:'Something went wrong: '+ err}); //returns this
}
else{
res.json({success:true,message:"Account updated !"});
}
});
}
}
});
}
}
}
}
});

编辑

这是密码验证器数组

const passwordValidators = [
{
validator: passwordLengthChecker,
message: 'Password must be at least 5 characters but no more than 40'
},
{
validator:validPassword,
message: 'Must have at least one uppercase, lowercase, special character, and number'
}
];

和跳棋

let passwordLengthChecker = (password)=>{
if (!password) {
return false;
}
else{
if(password.length<5 || password.length>40){
return false;
}
else{
return true;
}
}
};

let validPassword = (password)=>{
if (!password) {
return false;
}
else{
const regExp = new RegExp(/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[\d])(?=.*?[\W]).{8,35}$/);
return regExp.test(password);
}
};

正如您所看到的,它正在使用密码长度检查器,尽管它不应该使用

编辑 N°2

我刚刚意识到我在架构下面有这个中间件

userSchema.pre('save', function(next){
if(!this.isModified('password'))
return next();

bcrypt.hash(this.password, null, null, (err,hash)=>{
if(err) return next(err);
this.password=hash;
next();
});
});

这是否意味着每次我使用 save() 时该函数都会运行?

最佳答案

问题就在这里,您提供了 biolocationgenderbirthday 但没有提供密码。当您不声明密码时,其长度将等于0。这就是为什么你会收到错误。长度可以从 540

else{
user.password=req.body.password;
//I added user.password here, this is what you should do
user.bio=req.body.bio;
user.location=req.body.location;
user.gender=req.body.gender;
user.birthday=req.body.birthday;
user.save((err)=>{
if(err){
res.json({success:false,message:'Something went wrong: '+ err}); //returns this
}
else{
res.json({success:true,message:"Account updated !"});
}
});
}

在此更新

如果您只想更新biolocationgenderbirthday,那么使用save()函数是错误的。您需要使用findOneAndUpdate()函数。

User.findOneAndUpdate({_id:req.decoded.userId}, { $rename : {gender: 'male' , bio : 'somethingElse'}}, {new: true}, function(err, user){
if(err) throw err;
else{
console.log("done : " + user.gender);
}
});

另请参阅 findOneAndUpdate its operators to be used

关于node.js - Mongoose 中使用错误验证器的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com