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.

Update instance attribute in "beforeUpdate" hook

See original GitHub issue

Hello,

The beforeUpdate hook on single update (not related to individualHook) does not update the query

Link to https://github.com/sequelize/sequelize/issues/5821 but on single update (with .save())

What are you doing?

      beforeUpdate: (instance) => {
        if (instance.changed('plainPassword')) {
          instance.encryptedPassword = hashPassword(instance.plainPassword);

          //instance.setDataValue('encryptedPassword', hashPassword(instance.plainPassword)); Tried with this syntax as well
        }
      },

What do you expect to happen?

Update the SQL query via the beforeUpdate hook

What is actually happening?

The SQL query executed

[0] Executing (default): UPDATE "user" SET "updated_at"='2019-01-07 15:41:27.684 +00:00' WHERE "id" = 'a70a0948-1061-4074-a7fb-f46fd974bb6d'

Dialect: postgres Dialect version: 7.7.1 Database version: 11 Sequelize version: 4.41.2 Tested with latest release: Yes

Thanks a lot

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sushantdhimancommented, Jan 15, 2019

So beforeValidate and beforeUpdate are not working together

0reactions
cbarlow1993commented, Jul 16, 2020

Having similar issues here whereby I only have a beforeUpdate hook and trying to add a field in to update,

ReadyToDispatchTimestamp is set in beforeUpdate hook. At the bottom of the beforeUpdate hook, i’ve run instance.isChanged() and it returns as [ ‘State’, ‘Note’, ‘ReadyToDispatchTimestamp’ ].

I would expect that ReadyToDispatchTimestamp would also be in the executed SQL statement but I get none.

Executing (default): UPDATE Order SET PaymentType=?,CartTotal=?,State=?,CodiceFiscale=?,CouponCode=?,Note=?,updatedAt=? WHERE Id = ?

I’ve tried making ReadyToDispatchTimestamp a string field and also a Date field, neither worked.

Lost…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to assign or update value in beforeUpdate hook with ...
I'm trying to update a database instance using beforeUpdate hook (beforeBulkUpdate actually) and the hook is triggered, but it won't update ...
Read more >
Hooks - Sequelize
Arguments to hooks are passed by reference. This means, that you can change the values, and this will be reflected in the insert...
Read more >
Hooks - Sequelize - W3cubDocs
Instance hooks. The following hooks will emit whenever you're editing a single object beforeValidate afterValidate or validationFailed beforeCreate / ...
Read more >
Sequelize hooks explained with code examples
You can do so by adding an afterCreate hook to the model that represents the Users table above. Here's one example of adding...
Read more >
A Complete Guide to Vue Lifecycle Hooks - with Vue 3 Updates
For example, you can remove event listeners. beforeUpdate could be useful for tracking the number of edits made to a component or even...
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