Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 密码bcrypt返回未定义_Javascript_Node.js_Sequelize.js_Bcrypt - Fatal编程技术网

Javascript 密码bcrypt返回未定义

Javascript 密码bcrypt返回未定义,javascript,node.js,sequelize.js,bcrypt,Javascript,Node.js,Sequelize.js,Bcrypt,我试图用bcrypt加密密码,但是当我调用this.setDataValue('password',hash) 由于ValidationError(非null),返回变得未定义且不保存,但在上面一行的console.log中对其进行了哈希处理。 我尝试将es6 arrow函数改为function(),但正如预期的那样,我无法访问this.setDataValue。 有人能给我点火吗 const { Sequelize } = require('sequelize'); const {databa

我试图用bcrypt加密密码,但是当我调用
this.setDataValue('password',hash)
由于ValidationError(非null),返回变得未定义且不保存,但在上面一行的console.log中对其进行了哈希处理。 我尝试将es6 arrow函数改为function(),但正如预期的那样,我无法访问
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));
            }
        }
    }

})