Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SailsJS: beforeUpdate (Lifecycle callback), access current values

It's clear how to access the new values, the first argument contains them

beforeUpdate: function (values, cb) {....}

But how can I access the current values, that are to be replaced?

like image 816
Alon Amir Avatar asked Mar 10 '26 21:03

Alon Amir


1 Answers

In order do access current values you will have to make the call and get the current record.

models/user.js
beforeCreate: function (values, cb) {
    User.findOne(this.update.arguments[0].id || this.update.arguments[0]).exec(err, function(originalUser){/*...*/})
}

Also check out https://github.com/balderdashy/waterline/issues/1004#issuecomment-102432862 , where this issue is being discussed. Should provide insight into getting the ID and a current solution.

The solution above was updated to reflect the comments.

like image 52
Meeker Avatar answered Mar 13 '26 09:03

Meeker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!