首页 > 解决方案 > Sequelize - afterBulkUpdate() 如果更新与否如何获取

问题描述

如果实例用户更新与否,我如何知道在一个钩子 afterBulkUpdate() 内?

UserController.js

const [isUpdated] = await User.update(
    fields,
    {where}
);

// Where I know if updated or not! isUpdated is 1 when updated, 0 when not updated

User.js

// But inside a Model how I know if updated or not?
User.addHook('afterBulkUpdate', (options) => {
    // user.isUpdated?
});

我不能使用 afterUpdate(),没有运行到我,即使individualHooks: true像这样使用:

UserController.js

const [isUpdated] = await User.update(
    fields,
    {where, individualHooks: true}
);

我正在使用 MySQL。有什么解决办法吗?

标签: mysqlnode.jssequelize.js

解决方案


const user = sequelize.define("user",
    {
      first_name: { type: DataTypes.STRING, trim: true },
      ...
     
    },
      hooks: {
        // afterUpdate(instance, options) {
        //   let changed = instance._changed;
        //   console.log(changed, options);
        // }
      },
    }
  );

推荐阅读