【问题标题】:Sequelize transaction stops executing during loopSequelize 事务在循环期间停止执行
【发布时间】:2018-08-17 10:20:01
【问题描述】:

我正在尝试做一个循环遍历多个项目并将它们插入数据库的事务。如果我在每个数组中只有 1 项,则代码执行良好,它会插入部署、工作人员和设备。但是,如果我的设备或船员中有 2 件或更多物品,应用程序就会冻结,没有错误或任何东西。

控制台输出如下所示:

Executing (e337b7de-e95f-4d18-a2e9-1216cb8b7d61): START TRANSACTION;
----------------------CREATE DEPLOYMENT---------------
Executing (e337b7de-e95f-4d18-a2e9-1216cb8b7d61): INSERT INTO `deployments` (`id
`,`dateDeployed`,`dateReturned`,`city`,`province`,`country`,`fireName`,`fireNumb
er`,`fireCenter`,`unitNumber`,`comments`,`finalSold`,`status`,`createdAt`,`updat
edAt`,`contractId`,`deploymentTypeId`,`productId`) VALUES (DEFAULT,'2018-03-01',
'Invalid date','1','BC','CAN','1','1','1','1','test','','active','2018-03-08 22:
36:44','2018-03-08 22:36:44','5','1','1');
----------------------CREATE EQUIPEMENT---------------
----------------------CREATE EQUIPEMENT---------------
Executing (default): INSERT INTO `deploymentEquipments` (`createdAt`,`updatedAt`
,`deploymentId`,`equipmentId`) VALUES ('2018-03-08 18:09:31','2018-03-08 22:36:4
4',17,1);
Executing (default): INSERT INTO `deploymentEquipments` (`createdAt`,`updatedAt`
,`deploymentId`,`equipmentId`) VALUES ('2018-03-08 18:09:39','2018-03-08 22:36:4
4',17,2);

我的代码是这样的:

app.post('/deployment', function(req,res,next){

  var deployment = req.body;
  var crew = req.body.deploymentCrew;
  var equipment = req.body.deploymentEquipment;
  var deploymentId = "";


    //insert new deployment - start transaction, add deployment, get ID, loop through crew, loop through equipment
    models.sequelize.transaction(t =>
    {
      var equipPromises = [];
      console.log('----------------------CREATE DEPLOYMENT---------------');
      return models.deployment.create(req.body, {transaction: t})
        .then(function(newDeployment) {
          deploymentId = newDeployment.dataValues.id;
          for (var i = 0; i < equipment.length; i++) {
            console.log('----------------------CREATE EQUIPEMENT---------------');
            var equip = equipment[i];
            equip.deploymentId = deploymentId;
            equip.equipmentId = equipment[i].id;
            var equipPromise = models.deploymentEquipment.create(equip, equipPromises.push(equipPromise));
          }
          return Promise.all(equipPromises);
        })
        .then(function() {
          console.log('----------------------CREATE STAFF---------------');
          var crewPromises = [];
          for (var i = 0; i < crew.length; i++) {
            var staff = crew[i];
            staff.deploymentId = deploymentId;
            staff.staffId = crew[i].staff.id;
            var crewPromise = models.deploymentCrew.create(staff, crewPromises.push(crewPromise));
          }
          return Promise.all(crewPromises);
        });


    }).then(result => {
      console.log('deployment added');
      res.send(result);
    }).catch(err => {
      console.log('deployment creation failed');
      res.status(401).send({'message':err, 'redirect': '/'});
    });

});

任何可能发生这种情况的想法或想法将不胜感激。

谢谢

【问题讨论】:

    标签: javascript express sequelize.js


    【解决方案1】:

    实际上很简单,我没有将事务添加到循环创建语句中。所以现在是这样的:

    //this is the old statement
    //var equipPromise = models.deploymentEquipment.create(equip, equipPromises.push(equipPromise));
    //this is the new correct way to do it
                equipPromises.push(models.deploymentEquipment.create(equip, {transaction:t}));
    

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多