密码 bcrypt return 未定义
Password bcrypt return undefined
我正在尝试使用 bcrypt 加密密码,但是当我调用 this.setDataValue('password',hash)
由于 ValidationError(非空),return 变为未定义且不保存,但在上面一行的 console.log 中它被散列。
我尝试改为更改函数 () 的 es6 箭头函数,但正如预期的那样,我无法访问 this.setDataValue
。
谁能给我一盏灯?
const { Sequelize } = require('sequelize');
const {database,username,password,host } = require('./dbcon');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const sequelize = new Sequelize(database , username, password, {host:host ,dialect: 'mariadb'});
const user = sequelize.define('users',{
mail:{
type:Sequelize.STRING,
allowNull:false
},
firstName:{
type:Sequelize.STRING,
allowNull:false,
},
lastName:{
type:Sequelize.STRING,
allowNull:false,
},
password:{
type:Sequelize.STRING,
allowNull:false,
set(value) {
bcrypt.hash(value,saltRounds).then(f(hash)=>{
console.log(value,hash);
this.setDataValue('password',hash);
});
}
},
username:{
type:Sequelize.STRING,
allowNull:false,
}
})
try {
sequelize.authenticate().then(res =>{
console.log('Connection has been established successfully.');
user.sync({ force: true });
});
} catch (error) {
console.error('Unable to connect to the database:', error);
}
module.exports = {sequelize,user};
您可以使用 beforeCreate
hook 和 bcrypt 异步方法或 instanceMethods
在选项中:
const user = sequelize.define('users',{
mail:{
type:Sequelize.STRING,
allowNull:false
},
firstName:{
type:Sequelize.STRING,
allowNull:false,
},
lastName:{
type:Sequelize.STRING,
allowNull:false,
},
password:{
type:Sequelize.STRING,
allowNull:false
},
username:{
type:Sequelize.STRING,
allowNull:false,
}
}, {
instanceMethods: {
generateHash(password) {
return bcrypt.hash(password, bcrypt.genSaltSync(8));
}
}
}
})
我正在尝试使用 bcrypt 加密密码,但是当我调用 this.setDataValue('password',hash)
由于 ValidationError(非空),return 变为未定义且不保存,但在上面一行的 console.log 中它被散列。
我尝试改为更改函数 () 的 es6 箭头函数,但正如预期的那样,我无法访问 this.setDataValue
。
谁能给我一盏灯?
const { Sequelize } = require('sequelize');
const {database,username,password,host } = require('./dbcon');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const sequelize = new Sequelize(database , username, password, {host:host ,dialect: 'mariadb'});
const user = sequelize.define('users',{
mail:{
type:Sequelize.STRING,
allowNull:false
},
firstName:{
type:Sequelize.STRING,
allowNull:false,
},
lastName:{
type:Sequelize.STRING,
allowNull:false,
},
password:{
type:Sequelize.STRING,
allowNull:false,
set(value) {
bcrypt.hash(value,saltRounds).then(f(hash)=>{
console.log(value,hash);
this.setDataValue('password',hash);
});
}
},
username:{
type:Sequelize.STRING,
allowNull:false,
}
})
try {
sequelize.authenticate().then(res =>{
console.log('Connection has been established successfully.');
user.sync({ force: true });
});
} catch (error) {
console.error('Unable to connect to the database:', error);
}
module.exports = {sequelize,user};
您可以使用 beforeCreate
hook 和 bcrypt 异步方法或 instanceMethods
在选项中:
const user = sequelize.define('users',{
mail:{
type:Sequelize.STRING,
allowNull:false
},
firstName:{
type:Sequelize.STRING,
allowNull:false,
},
lastName:{
type:Sequelize.STRING,
allowNull:false,
},
password:{
type:Sequelize.STRING,
allowNull:false
},
username:{
type:Sequelize.STRING,
allowNull:false,
}
}, {
instanceMethods: {
generateHash(password) {
return bcrypt.hash(password, bcrypt.genSaltSync(8));
}
}
}
})