beforeupdate sequelize does not work

1

I'm trying to make the data update encrypt the user's password, but it simply does not work and does not give an error. Have the following sequelize model.

import bcrypt from 'bcrypt';

export default (sequelize, DataType) => {
  const Users = sequelize.define('Users', {
    id: {
      type: DataType.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },

    name: {
      type: DataType.STRING,
      allowNull: false,
      validation: {
        notEmpty: true,
      },
    },
    jobTitle: {
      type: DataType.STRING,
      allowNull: false,
      validation: {
        notEmpty: true,
      },
    },

    login:{
      type:DataType.INTEGER,
      allowNull:false,
      validation:{
        notEmpty:true
      }
    },

    password:{
      type:DataType.STRING,
      allowNull:false,
      validation:{
        notEmpty:true
      },
    },
  },
{
  hooks:{
    beforeCreate: user => {
      const salt = bcrypt.genSaltSync();
      user.set('password', bcrypt.hashSync(user.password, salt))
    },
    beforeUpdate: user => {      
      const salt = bcrypt.genSaltSync();
      user.set('password', bcrypt.hashSync(user.password, salt))
    },
  },
  classMethods:{
    isPassword:(encodedPassword, password) => bcrypt.compareSync(password, encodedPassword)
  }
});
  return Users;
}

create works perfectly, however in the update nor does it perform the update.

    
asked by anonymous 28.11.2016 / 06:36

1 answer

0

The beforeUpdate depends on how the update is performed, since it only works on instance.save() or instance.update(values) .

For model.update(values, options) use beforeBulkUpdate .

    
03.04.2017 / 19:57