" upsertWithWhere"正在无限次地奔跑

时间:2017-12-15 11:11:46

标签: javascript node.js loopbackjs

我正在尝试使用upsertWithWhere来更新数据库中的字段,这也是正在发生的事情,但是如果我正在使用break语句,那么代码运行无限次我会收到错误"非法中断语句&#34 ;,我尝试使用process.exit(),但没有运气

  CampaignDetail.observe("after save", function(ctx, cb) {
    console.log("inside save");
    let sourceConditionCampaignId = ctx.instance.sourceConditionCampaignId;
    if (sourceConditionCampaignId != null) {
      CampaignDetail.find({
        where: { id: sourceConditionCampaignId }
      })
        .then(campaign => {
          console.log("inside campaign");
          let campaignConditionJSONValue = campaign[0].campaignConditionJSON;
          let cmd = ctx.instance;
          cmd.campaignConditionJSON = campaignConditionJSONValue;
          return cmd;
        })
        .then(updateCampaign => {
          console.log(updateCampaign);
          console.log("inside update campaign");
          let campId = ctx.instance.id;
          CampaignDetail.upsertWithWhere({ id: campId }, updateCampaign);
          return updateCampaign;
        })
    } else {
      console.log("selected campaign doesnt have the json");
    }
    let response = "stored";
    cb(null, response);
  });

1 个答案:

答案 0 :(得分:0)

这就是为什么upsertWithWhere再次调用'after save'函数的钩子的原因。 保存挂钩功能后还有其他几个函数可以调用,在这种情况下,你可以看到相同的无限循环。

  

此挂钩由PersistedModel的以下方法触发:   find()findOne()findById()exists()count()create()upsert()(相同)   as updateOrCreate())upsertWithWhere()findOrCreate()*   prototype.save()prototype.updateAttributes()replaceOrCreate()   prototype.replaceAttributes()/ replaceById()

https://loopback.io/doc/en/lb3/Operation-hooks.html#after-save