在“请求POST的未处理的错误”之后,对一个特定的剩余URL进行环回服务器超时

时间:2019-04-18 22:40:14

标签: node.js loopback

我在回送服务器中创建钩子之前定义了一个,并尝试进行一些错误处理。业务逻辑包括:接收到的请求参数(包括开始和结束日期)不应与数据库中存储的任何日期范围重叠。在那种情况下,我抛出一个错误。 但是,在引发错误之后,对该URL的所有其他请求都将超时。 虽然,服务器仍可以在其他URL上完美运行。

Unhandled error for request POST /api/MCarReservations: Error: Car is already reserved
    at C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\common\models\m-car-reservation.js:53:35
    at notifyObserversAround.err (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\transaction.js:190:11)
    at C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\observer.js:250:22
    at doNotify (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
    at Transaction.ObserverMixin._notifyBaseObservers (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
    at Transaction.ObserverMixin.notifyObserversOf (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
    at cbForWork (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-datasource-juggler\lib\observer.js:240:14)
    at Query.callback (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\loopback-connector-postgresql\lib\transaction.js:68:7)
    at Query.handleReadyForQuery (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\pg\lib\query.js:125:10)
    at Connection.<anonymous> (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\pg\lib\client.js:215:19)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (C:\Fakultet\6. semestar\ISAMRS\Projekat\isamrs13\ahref-backend\node_modules\pg\lib\connection.js:125:12)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)

'use strict';


var flag = true;
// SQL car reservation before-hook
module.exports = function(Mcarreservation) {
  Mcarreservation.beforeRemote('create',
    function(ctx, model, next) {
      doReservation(Mcarreservation, ctx, model, next, function(e) {
        flag = false;
        next(e);
      });
    });
};

function doReservation(Mcarreservation, ctx, model, next, errorCallback) {
  // models
  var sqlCarReservation = Mcarreservation.app.models.CarReservation;
  var sCar = Mcarreservation.app.models.sCar;
  // data source
  var postgres = sCar.app.dataSources.postgres;
  // begin transaction
  sqlCarReservation.beginTransaction({
    isolationLevel: sqlCarReservation.Transaction.READ_COMMITTED,
  }, function(err, tx) {
    if (err) errorCallback(err);
    // lock car for update
    postgres.connector.execute(
      'SELECT * FROM sCar WHERE mongoId = $1 FOR UPDATE;'
      , [ctx.req.body.carId], function(err, data) {
        sCar.findOne({where: {mongoId: ctx.req.body.carId}}).then((car)=>{
          sqlCarReservation.find(
            {
              where: {sCarId: car.id},
            }).then((data)=> {
              data.forEach((element) => {
                if (flag) {
                  var start1 = element.startDate.getTime();
                  var end1 = element.endDate.getTime();
                  var start2 = ctx.req.body.startDate;
                  var end2 = ctx.req.body.endDate;
                  if ((start1 >= start2 && start1 <= end2) ||
                    (start2 >= start1 && start2 <= end1)) {
                    tx.rollback(function(err) {
                      if (err && flag) errorCallback(err);

                      var error = new Error('Car is already reserved');
                      error.statusCode = error.status = 404;
                      if (flag)
                        errorCallback(error);
                    });
                  }
                }
              });

              if (err && flag) {
                tx.rollback(function(err) {
                  if (err && flag) errorCallback(err);
                });
                errorCallback(err);
              }
              // fetch car from sql
              if (flag) {
                sCar.findOne({where: {mongoId: ctx.req.body.carId}})
                  .then((car)=>{
                    // create reservation
                    sqlCarReservation.create(
                      {
                        timeStamp: ctx.req.body.timeStamp,
                        sCar: car,
                        startDate: ctx.req.body.startDate,
                        endDate: ctx.req.body.endDate,
                      },
                      {transaction: tx},
                      function(err, cr) {
                        if (err && flag) {
                          tx.rollback(function(err) {
                            if (err && flag)  errorCallback(err);
                          });
                          errorCallback(err);
                        }
                        // commit and end before-hook
                        if (flag) {
                          tx.commit(function(err) {
                            if (err && flag)  errorCallback(err);
                            next();
                          });
                        }
                      });
                  });
              }
            });
        });
      });
  });
}

我知道代码看起来就像恶魔自己写的一样,我真的希望有人能理解它。 预期结果是服务器返回404,并显示消息“汽车已被保留”,并继续运行。

0 个答案:

没有答案