在Mongoose中使用错误的验证器路由

时间:2017-08-22 02:31:55

标签: node.js mongodb mongoose

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

我做错了什么?

mongoose中的用户架构:

passwordValidator

路线:

    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'
    }
];

正如您所看到的那样,它使用的是密码长度检查器,但它不应该

编辑N°2

我刚刚意识到我的架构正好位于架构

之下
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);
    }
};

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

2 个答案:

答案 0 :(得分:1)

向下是问题,您提供biolocationgenderbirthday但不提供密码。如果您未声明密码,则length将等于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

答案 1 :(得分:0)

所以我认为bcrypt函数破坏了Mongoose save()函数, 我现在在Mongoose doc中找到了另一个解决方案。

User.findByIdAndUpdate(req.decoded.userId,{$set:{bio:req.body.bio, location:req.body.location, gender:req.body.gender, birthday:req.body.birthday}},{new:true},function(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{
                                    res.json({success:true,user:user});
                                }
                            }
                        });