简体   繁体   English

如何通过选项来续集 afterBulkUpdate

[英]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.

相关问题 如何使用Sequelize将ID传递到其关联记录 - How to pass id to its associated records with Sequelize 如何将 req 值传递到 Sequelize findAll - How to pass req values into Sequelize findAll 当参数描述关系时如何将查询参数传递给续集 - How to pass query params into sequelize when params describe relations 传递给 findOne 的参数必须是一个选项对象,如果您希望传递单个主键值,请使用 findById 我在 Sequelize 中已完成此操作 - The argument passed to findOne must be an options object, use findById if you wish to pass a single primary key value I have done this in Sequelize Sequelize.js Node.js:如何传递已经创建的对象来创建多对多关系? - Sequelize.js Node.js: How to Pass Already Created Object to Create Many-to-Many relationship? 如何将Sequelize查询的输出传递给另一个函数,该函数将返回带有express的JSON对象? - How to pass the output of a Sequelize query to another function that will return a JSON object with express? 如何在节点表达选项qs中传递多个值? - How to pass multiple values in node express options qs? 使用ES6导入/导出时,如何通过快速路由将续集传递到节点中MVC model中的controller? - How to pass sequelize through express routes to controller in MVC model in node when using ES6 import/export? 如何在cookie会话1.0.2中传递cookie选项 - How to pass cookie options in cookie-session 1.0.2 如何扩展Sequelize模型? - How to extend a Sequelize model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM