Update instance attribute in "beforeUpdate" hook
See original GitHub issueHello,
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:
- Created 5 years ago
- Comments:10 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So
beforeValidate
andbeforeUpdate
are not working togetherHaving 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
SETPaymentType
=?,CartTotal
=?,State
=?,CodiceFiscale
=?,CouponCode
=?,Note
=?,updatedAt
=? WHEREId
= ?I’ve tried making ReadyToDispatchTimestamp a string field and also a Date field, neither worked.
Lost…