question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attributes modified in hooks are not saved

See original GitHub issue

If you add a hook to (for exemple) a User model, and save an instance of the model with some attributes modified, such as:

user.email = 'toto@toto.com';
user.save();

And if you modify some user attributes into the (for exemple…) afterValidate hook:

function afterUserValidate(user) {
  if(user.changed('email')) {
    return u.md5(user.email).then((md5) => user.emailMd5 = md5);
  }
}

The emailMd5 attribute is not saved. This is because dirty attributes are computed before the pre-save hooks.

So my question: could we imagine to compute dirty attributes after the pre-save hooks?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:6
  • Comments:41 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
francislicommented, Jul 21, 2020

After digging further, for anyone else who ends up here, it is by design that the beforeUpdate (including beforeSave, on an existing record) is NOT allowed to change additional attributes that were not previously changed before the update/save call.

You can see the code in the current master branch starting here: https://github.com/sequelize/sequelize/blob/bb30483a95658bc1e729d5ae2a966f86872f955d/src/model.js#L4108

It collects a list of attributes in which it will ignore changes (ignoreChanged), which is all the configured attributes not already changed when the update/save is called. It uses that list to filter out any additional changes made during the execution of the hook.

You can override this behavior by manually adding the changed fields to options.fields before your hook function returns.

6reactions
akengercommented, Nov 29, 2017

This bug still exists in 4.22.12. If you set an attribute in a beforeUpdate hook that was not part of the original list of update fields, the updated field appears in the response but not in the database. You can’t call .save in a beforeUpdate hook b/c it creates an endless loop.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React hooks not updating components - Stack Overflow
It's not updating your component because react doesn't think your state has changed from the previous render as you're modifying your state ...
Read more >
How to Use Formidable Hooks
frm_images_dropdown_option_classes: This hook allows to modify the CSS classes of each option in FrmAppHelper::images_dropdown() method.
Read more >
useFieldArray - Simple React forms validation
Custom hook for working with Field Arrays (dynamic form). ... Name of the attribute with autogenerated identifier to use as the key prop....
Read more >
Mongoose v6.8.1: Middleware
pre('updateOne', { document: true, query: false }) . Note: The create() function fires save() hooks. Note: Query middlewares are not executed on subdocuments....
Read more >
Operation hooks | LoopBack Documentation
Using operation hooks enables you to intercept actions that modify data ... NOTE: When findOrCreate finds an existing model, the save hooks are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found