【问题标题】:Sequelize transaction always returning nullSequelize 事务总是返回 null
【发布时间】:2019-09-24 21:57:52
【问题描述】:

我正在编写用于创建审计协议的后端。用户应该能够为审计协议创建标准。为此,我有以下后端方法来确保仅完全创建协议或取消创建过程。可以设置多种形式/标准。但也有可能只需要一种形式。我确实使用 if 语句进行检查。

创建按预期工作。但是 REST API 总是返回 null 给客户端。所以我不能在前端对创建过程的结果做进一步的处理。

技术:Node.js 和 Sequelize。角度/离子的前端。 mySQL 中的数据库。

我尝试了一些事务传递和返回语句。我试图将它与类似的代码 sn-p 进行比较,它按预期工作。

exports.setAudit = (req, res, next) => {
  trueFalseCriteria = req.body.trueFalseForms;
  isShouldCriteria = req.body.isShouldForms;
  generalCriteria = req.body.generalForms;
  measurementCriteria = req.body.measurementForms;
  toolId = req.body.toolId;

  // Transaction is used to roll the whole transaction back if something wents wrong
  return sequelize
    .transaction(t => {
      return audit
        .create(
          {
            // Creating an audit referencing the tool
            toolId: toolId
          },
          { transaction: t }
        )
        .then(
          // Getting the id of the audit that we just created
          audit => {
            return audit.id;
          },
          { transaction: t }
        )
        .then(auditId => {
          // Check wether the kind of form is used or not. If so, sequelize tries to do a bulk insert into the databases.
          // Each bulk insert throws an error if it fails to cancel the whole transaction
          if (trueFalseCriteria) {
            console.log(1);
            trueFalseCriteria.forEach(dataEl => {
              dataEl.auditId = auditId;
            });
            trueFalseCriterion.bulkCreate(trueFalseCriteria).catch(err => {
              // Throw error to cancel transaction
              throw new Error(err);
            });
          }
          if (isShouldCriteria) {
            console.log(2);
            isShouldCriteria.forEach(dataEl => {
              dataEl.auditId = auditId;
            });
            isShouldCriterion.bulkCreate(isShouldCriteria).catch(err => {
              // Throw error to cancel transaction
              throw new Error(err);
            });
          }
          if (generalCriteria) {
            console.log(3);
            generalCriteria.forEach(dataEl => {
              dataEl.auditId = auditId;
            });
            generalCriterion.bulkCreate(generalCriteria).catch(err => {
              // Throw error to cancel transaction
              throw new Error(err);
            });
          }
          if (measurementCriteria) {
            console.log(4);
            measurementCriteria.forEach(dataEl => {
              dataEl.auditId = auditId;
            });
            measurementCriterion.bulkCreate(measurementCriteria).catch(err => {
              // Throw error to cancel transaction
              throw new Error(err);
            });
          }
        }, { transaction: t });
    })
    .then(data => {
      console.log(5);
      res.status(200).json(data);
    })
    .catch(err => {

      console.log(6);
      if (!err.statusCode) {
        err.statusCode = 500;
      }
      next(err);
    });
};

预期结果:成功时状态码为 200 的 Http 响应 实际结果:null

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    我认为你错过了最后一个.then()return

    .then(auditId => {
       // Check wether the kind of form is used or not. If so, sequelize tries to do a bulk insert into the databases.
       .....
       if (measurementCriteria) {
           ....
       }
    // RETURN SOMETHING HERE 
    }, { transaction: t });
    

    【讨论】:

    • 谢谢,@KenOn10。不知道我怎么忽略了这么久。此外,我必须将返回:true 设置为 bulkCreate。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2016-11-02
    • 2014-05-06
    • 2012-06-05
    • 2014-05-30
    • 2014-02-23
    相关资源
    最近更新 更多