问题
I have tried an example from http://apidocs.strongloop.com/loopback/#model:
MyModel.on('changed', function(inst) {
console.log('model with id %s has been changed', inst.id);
// => model with id 1 has been changed
});
I replaced MyModel
with actual model name. When I save a new instance or update an existing one, I expect to see a console log, but nothing happens.
How do I make it work as it's described in the docs?
回答1:
As I am using Loopback 3.0, not 2.0, the mentioned listener is deprecated and when I changed it to the example below, it helped and works fine.
MyModel.observe('after save', (inst) => {
console.log('model with id %s has been changed', inst.id);
});
Here is more information:
- http://loopback.io/doc/en/lb3/Migrating-to-3.0.html#replace-removed-persistemodel-event-listeners
- http://loopback.io/doc/en/lb2/Operation-hooks.html
来源:https://stackoverflow.com/questions/40524616/how-to-make-loopback-models-events-work