allfix
allfix

Reputation: 52

Can hook beforeUpdate/afterUpdate modify the value?

I have model call "point", I want to update field "updated" on point in every action update. So, I make hook boforeUpdate to modify updated with new Date(). In result callback is true, and updated fied is a new datetime. But not updated on db, the field still old datetime. My code:

var app = require('../../../server.js'),
point = app.models.point;

point.beforeUpdate = function(next) {
  var app = this;
  app.updated = new Date();
  next();
}

Upvotes: 0

Views: 565

Answers (1)

Warszk
Warszk

Reputation: 136

beforeUpdate callback takes two parameters: a callback (which is next in your example) and an object (your point instance)

var app = require('../../../server.js'),
point = app.models.point;

point.beforeUpdate = function(next, pt) {
  pt.updated = new Date();
  next();
}

Assuming that your code lies in app/models/point.js it can be simplified to:

module.exports = function(Point) {
  Point.beforeUpdate = function(next, point) {
    point.updated = new Date();
    next();
  }
};

Upvotes: 1

Related Questions