How to pass options to sequelize afterBulkUpdate

574 views Asked by At

I'm updating user but then after i need to also update the user_profile table. The user table gets updated but in afterBulkUpdate hook the options is undefined and therefore cannot be destructured and throws an error. How can i pass options while using Model.update and access them in afterBulkUpdate options. I know i'm passing the options the wrong way, how can i do it right. This is how i'm doing it

User.update({ password : newPassword, phone_number: phoneNumber 
   }, {  where: { username } } , { userOptions: { avatar, nationality, username} } );

Then in afterBulkUpdate i want to update the user profile table.

  User.afterBulkUpdate(async (user, options) => {
    console.log("user is: ", user); // user is not null
    console.log("options are: ", options); // options is undefined
    const { userOptions} = options;
    const { avatar, nationality, username} = userOptions;

    UserProfile.update({ avatar, nationality} ,{ where: { username }  });

  });
1

There are 1 answers

0
K.Nehe On

After looking at this question. I made my own hack to solve the problem, it works, i don't know if its recommended or will have future issues, please advice just in case.

I changed the update to

User.update({ password : newPassword, phone_number: phoneNumber 
   }, {  where: { username } , userOptions: { avatar, nationality, username} }  );

The userOptions and where clause are passed to the hook as objects in one object, so i changed the afterBulkCreate to

User.afterBulkUpdate(async (user, options) => {
    const { userOptions } = user;
    const { avatar, nationality, username} = userOptions;
    UserProfile.update({ avatar, nationality} ,{ where: { username }  });
  });