保存后不调用Loopback.js

时间:2018-07-19 09:23:40

标签: javascript node.js loopbackjs

所以我有以下代码:

loopbackModels.findModel("academy_automatic_event").find({
  where: {
    automatic_event_type: 1,
    active: true
  },
  include: {
    academy: ['versions']
  }
}).then(function(result) {
  let academyCreateArray = [];
  if (result && result.length > 0) {
    for (let i = 0; i < result.length; i++) {
      academyCreateArray.push({
        member_id: userInstance.id,
        academy_id: result[i].academy_id,
        academy_version_id: result[i].__data.academy.__data.versions[result[i].__data.academy.__data.versions.length - 1].academy_version_id
      })
    }    loopbackModels.findModel("member_has_academy").create(academyCreateArray).then(function(result) {
      next();
    }).catch(function(err) {
      console.log(err)
    })
  } else {
    next();
  }
})

现在要摆脱的重要一件事是,我在模型member_has_academy中创建新实例。

因此,在.js中,我具有以下代码:

Memberhasacademy.afterRemote('after save', function(context, memberAcademyInstance, next) {
  loopbackModels.findModel("academy_activity").find({
    where: {
      academy_id: memberAcademyInstance.academy_id,
      academy_version_id: memberAcademyInstance.academy_version_id
    },
    order: 'sort_number ASC'
  }).then(function(result) {
    if (result != null && result.length > 0) {
      for (let i = 0; i < result.length; i++) {
        loopbackModels.findModel("member_has_academy_activity").create({
          academy_activity_id: result[i].id,
          member_id: memberAcademyInstance.member_id,
          is_complete: false,
          taken: false,
          sort_number: result[i].sort_number,
          is_open: result[i].sort_number === 1
        })
      }
    }
    next();
  });
});

但是,从未调用过该挂钩。请注意,我也尝试过使用after create,但结果相同。

谁能告诉我为什么会这样?

1 个答案:

答案 0 :(得分:0)

好的,经过一些挖掘,我实际上找到了答案:

我必须使用的是:

    Memberhasacademy.observe('after save', function(ctx, next) {