How to make Loopback models events work?

点点圈 提交于 2020-01-04 13:47:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!