User.hook('beforeCreate', (user, options, fn)=>{
var salt = bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt)=>{
return salt;
});
bcrypt.hash(user.password, salt, null, (err, hash)=>{
if(err) return next(err);
user.password=hash;
return fn(null, options);
});
执行时上面的代码给出了fn不是函数的错误 返回fn(null,options);如果注释程序正在运行,则POSTMAN显示加密密码,但不会保存到数据库。
我的控制器部分如下。
userController.save = function(req, res) {
models.users.create({
user_name: req.body.user_name,
user_address: req.body.user_address,
user_email: req.body.user_email,
password: req.body.password
})
.then((user)=>{
//console.log("Successfully created an user.");
//res.redirect("/users/show/"+user.user_id);
return res.status(200).send({success: true, msg: 'Successfully created new User.', result: user});
})
.catch((err)=>{
//console.log(err);
//res.render("../views/users/create");
return res.status(200).send({success: false, msg: 'Invalid request' , error : err});
});
};