1
Product.observe('after save', function(ctx, next){
    productHelper.calProduct(ctx.instance);
    next();
  });

I have above method to do something once a product is updated, but 'after save' is also trigger by create. How can I get trigger only by update but not create?

Many thanks!

loopback": "^3.22.0" "connector": "postgresql"

veenaMSL
  • 101
  • 1
  • 9
Manson
  • 37
  • 1
  • 6

2 Answers2

0

You can use Remote hooks.

for example:

Product.afterRemote('updateUser', (ctx, userRes, next) => { next() {...code ....} })

where updateUser is my remote method. Change it with your method name or prototype.

Ashwanth Madhav
  • 1,084
  • 1
  • 9
  • 21
  • Thank you for the reply. Maybe my question is not very clear. What I really want to do is find out how to distinguish between a CREATE operation and an UPDATE operation. According to LoopBack 3.x doc, only certain (memory, MongoDB, and MySQL) connectors support ctx.isNewInstance. With other connectors it is undefined. Unfortunately, I am using Postgresql which is not support isNewInstance. – Manson Jan 08 '20 at 12:01
0

Finally found out that Postgresql is support isNewInstance, the problem is solved.

Manson
  • 37
  • 1
  • 6