[英]How to pass options to sequelize afterBulkUpdate
I'm updating user but then after i need to also update the user_profile table.我正在更新用户,但之后我还需要更新 user_profile 表。 The user table gets updated but in afterBulkUpdate hook the options is undefined and therefore cannot be destructured and throws an error.
用户表得到更新,但在 afterBulkUpdate 挂钩中,选项未定义,因此无法解构并引发错误。 How can i pass options while using Model.update and access them in afterBulkUpdate options.
如何在使用 Model.update 时传递选项并在 afterBulkUpdate 选项中访问它们。 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.然后在 afterBulkUpdate 我想更新用户配置文件表。
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 } });
});
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 userOptions 和 where 子句作为 object 中的对象传递给钩子,所以我将 afterBulkCreate 更改为
User.afterBulkUpdate(async (user, options) => {
const { userOptions } = user;
const { avatar, nationality, username} = userOptions;
UserProfile.update({ avatar, nationality} ,{ where: { username } });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.