如何使Loopback模型事件起作用? [英] How to make Loopback models events work?

查看:51
本文介绍了如何使Loopback模型事件起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 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
});

我用实际的型号名称替换了MyModel.当我保存一个新实例或更新一个现有实例时,我希望看到一个控制台日志,但是什么也没有发生.

我如何按照文档中的说明进行操作?

解决方案

当我使用Loopback 3.0而不是2.0时,已弃用了上述侦听器,当我将其更改为以下示例时,它可以正常工作.

>

MyModel.observe('after save', (inst) => {
    console.log('model with id %s has been changed', inst.id);
});

这里是更多信息:

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?

解决方案

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:

这篇关于如何使Loopback模型事件起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆