loopback icon indicating copy to clipboard operation
loopback copied to clipboard

BeforeUpdate cann't set value?

Open Spark-star opened this issue 12 years ago • 9 comments

Hi ,erveryone. I set the 'lastUpdated' value in 'beforeUpdate'. when I used updateById to update record, the response record 'lastUpdated' is the time I changed the record, but in database the 'lastUpdate' is not changed. the response like this:{ ... "lastUpdated": "2014-11-06T08:04:59.646Z", ''' }, but in database is like this: { ... "lastUpdated": "2014-11-06T07:37:59.646Z", ''' } my code is:

Mymodel.afterUpdate = function(next) { this.lastUpdated = new Date(); next(); }; the API is : PUT /mymodel/:id Is it wrong?

THX

Spark-star avatar Nov 06 '14 08:11 Spark-star

You should do it in beforeUpdate instead of afterUpdate.

Thanks,


Raymond Feng Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop http://strongloop.com/ makes it easy to develop APIs http://strongloop.com/mobile-application-development/loopback/ in Node, plus get DevOps capabilities http://strongloop.com/node-js-performance/strongops/ like monitoring, debugging and clustering.

On Nov 6, 2014, at 12:14 AM, Spark-star [email protected] wrote:

Hi ,erveryone. I set the 'lastUpdated' value in 'beforeUpdate'. when I used updateById to update record, the response record 'lastUpdated' is the time I changed the record, but in database the 'lastUpdate' is not changed. the response like this:{ ... "lastUpdated": "2014-11-06T08:04:59.646Z", ''' }, but in database is like this: { ... "lastUpdated": "2014-11-06T07:37:59.646Z", ''' } my code is:

Mymodel.afterUpdate = function(next) { this.lastUpdated = new Date(); next(); }; the API is : PUT /mymodel/:id Is it wrong?

THX

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/755.

raymondfeng avatar Nov 06 '14 17:11 raymondfeng

I am sorry . I put the wrong code to the github. I tried beforeUpdate and afterUpdate , neither worked. Would you check it again? Thanks

Spark-star avatar Nov 15 '14 05:11 Spark-star

+1 beforeUpdate doesn't seem to work at all. LoopBack v.2.11.0

aantipov avatar Feb 02 '15 08:02 aantipov

We are moving away from beforeUpdate/afterUpdate hooks in favour of Operation hooks and Per-method hooks.

Please upgrade your project to use the new before save hook and open a new GH issue if you run into any problem.

MyModel.observe('before save', function(ctx, next) {
  if (ctx.instance) {
   ctx.instance.lastUpdated = new Date();
  } else {
    ctx.data.lastUpdated = new Date();
  }
  next();
});

bajtos avatar Feb 03 '15 15:02 bajtos

@bajtos why do you close the issue? There is a documentation on these hooks & they are not even marked as deprecated, so they should work.

aantipov avatar Feb 04 '15 11:02 aantipov

@aantipov We are going to deprecate the old hooks as soon as we gain enough confidence that the new hooks provide all functionality needed by LoopBack users to upgrade.

We are not going to fix the old hooks and almost certainly we won't accept pull request (changes) fixing these hooks either.

That's why I am asking all users that reported hook-releated bugs to try the new hooks and let us now if there is anything missing.

bajtos avatar Feb 04 '15 13:02 bajtos

huh It turns out that users should choose between

  1. new method, that possibly have some issues & it's documentation warns about using it. and
  2. old method, that is broken & will not be fixed. In my opinion it is rather strange behavior.

aantipov avatar Feb 04 '15 14:02 aantipov

@aantipov My point of view is different: you can use old hooks that have a set of know issues and limitations, or you can try out the new hooks that fixed known issues of the old hooks and don't have any other known issues.

I was very careful in designing the new hooks in a way that minimises any potential problems, you can check the extensive test suite which is accompanying the implementation (loopback-datasource-juggler/test/persistence-hooks.suite.js.

Basically my only concern is that there may be a use-case that we did not consider and which cannot be implemented using the new hooks as they are now. See for example https://github.com/strongloop/loopback/issues/838. The only way how to find that is to get LoopBack users try the new feature in their real-world applications and let us know what they found.

bajtos avatar Feb 04 '15 14:02 bajtos

@bajtos ok, thanks for the answer

aantipov avatar Feb 04 '15 14:02 aantipov