Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why sequelize beforeUpdate hook doesn't work?

I have a simple user model with 2 hooks.

User.beforeCreate(setSaltAndPass)
User.beforeUpdate(setSaltAndPass)

the first works perfectly but the beforeUpdate does not run, according to the documentation you should have no problem executing the following

await User.update(user, {
             where: {
               id
             }

         })

is saving the key as plain text, it does not happen for example with the create. Curiously beforeBulkUpdate is executed when it is updated.

this is the callback, the detail is that in bulk I don't have the changed property or at least I don't know how to access it.

const setSaltAndPass = user => {
   if (user.changed('pass')) {
     user.salt = User.generateSalt()
     user.pass = User.encriptPass(user.pass(), user.salt())
   }
}

does its job, but hook its not excecuted.

like image 881
Michel Novellino Avatar asked Mar 05 '26 06:03

Michel Novellino


1 Answers

Set individualHooks: true, in the options block

await User.update(user, {
  where: {
    id
  },
    individualHooks: true,
});
like image 144
Pugazhenthi Avatar answered Mar 07 '26 20:03

Pugazhenthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!