【问题标题】:loopback model hooks not working环回模型挂钩不起作用
【发布时间】:2015-02-03 17:09:10
【问题描述】:

我目前正在开发一个使用环回的预约应用程序,下面是我在 common/model 目录中的约会.js

module.exports = function(Appointment) {


  Appointment.afterCreate = function (next) {
      //TODO add appointment details should not be hardcoded get users email here
      var emailService = require('../../common/services/email.server.service');
      emailService.sendUserEmail('dummy@dummy.com');
      next();
  };

  Appointment.beforeUpdate = function(next) {
      var emailService = require('../../common/services/email.server.service');
      if(this.appointmentStatus === 'waiting_agent'){
          console.log(this.appointmentStatus);
      } else if(this.appointmentStatus === 'waiting_user'){
          console.log(this.appointmentStatus);
      } else {
          console.log(this.appointmentStatus);
      }

      emailService.sendUserEmail('dummy@dummy.com');

      next();

  };
};

afterCreate 函数运行良好并发送电子邮件,但是 afterUpdate/beforeUpdate 不起作用,我使用 Angular 作为前端并使用 Angular 环回生成服务,下面是更新函数:

function updateAppointment(listingId,newAppointmentInfo,status){
    Appointment.updateAll(
        {
            where:
            {
                listingId : listingId
            }
        },
        {
            "appointmentDate": newAppointmentInfo.selectedDate,
            "appointmentTime" : newAppointmentInfo.selectedTime,
            "appointmentStatus" : status
        },
        function (appointment){
            console.log(appointment);
        },
        function (err){
            console.log(err);
        }
    )
}

是因为我调用的是 /Appointment/update api,而不是 /Appointment 上的 UPDATE 吗?

【问题讨论】:

    标签: node.js rest loopbackjs


    【解决方案1】:

    不推荐使用模型挂钩,取而代之的是操作挂钩和按方法挂钩。见http://docs.strongloop.com/display/LB/Operation+hooks

    欲了解更多信息,请参阅https://github.com/strongloop/loopback/issues/366中的最后一条评论

    【讨论】:

      【解决方案2】:

      Loopback 在模型挂钩触发方面存在一些问题。 见问题https://github.com/strongloop/loopback-datasource-juggler/issues/202

      【讨论】:

      • 虽然链接的信息可能会提供有价值的信息,但对于搜索相同问题的 OP 和其他人来说,如果你能描述这些“一些问题”是什么以及你如何相信它们与所面临的问题有关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2017-02-02
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多