5

In Sails' Waternline I need to be able to compare the previous value with the new one and assign a new attribute under some condition. For example:

beforeUpdate: function(newValues, callback) {
   if(/* currentValues */.age > newValues.age) {
     newValues.permission = false;
   }
}

How could I access the currentValues ?

Talysson
  • 1,363
  • 1
  • 9
  • 13

1 Answers1

3

I'm not sure this is the best solution but you can get the current record by doing a simple findOne request:

beforeUpdate: function(newValues, callback) {
  Model
    .findOne(newValues.id)
    .exec(function (err, currentValues) {
      // Handle errors
      // ...

      if(currentValues.age > newValues.age) {
        newValues.permission = false;
      }

      return callback();
    });
}
Yann Bertrand
  • 3,084
  • 1
  • 22
  • 38
  • 1
    I was trying to avoid the extra `findOne`, but I guess it will work, thanks ! – Talysson Jul 19 '15 at 17:01
  • 3
    Note that this won't work if you're not passing along the `id` for the update or if you're updating multiple records based on criteria in stead of an `id`. – Wesley Overdijk Mar 19 '16 at 22:26