How to deal with Unhandled rejection SequelizeConnectionError: connect ETIMEDOUT

How to deal with Unhandled rejection SequelizeConnectionError: connect ETIMEDOUT

以下代码是一个项目,它解析 CSV 文件的目录以输入到 MySQL 数据库中。它通常有效,但是我被 ETIMEDOUT 错误所困扰。我是一名初级开发人员,不确定如何处理这种情况,或者为什么会发生这种情况。可能是因为我一次解析了太多信息。任何有关如何处理此问题的建议将不胜感激。

const app = require('express')();
var fs = require('fs');
var Papa = require('papaparse');
var Sequelize = require('sequelize');
var _ = require('underscore');
var mainDirectory = ['./Relay-Data'];

var sequelize = new Sequelize('relay_cloud','root','admin',{
  host: 'localhost',
  dialect: 'mysql',
  logging: false,
    pool: {
     max: 5,
     min: 0,
     idle: 50000,
     acquire: 50000,
     evict: 50000,
     handleDisconnects: true
 }
});

var Relay = sequelize.define('relay',{
  Timestamp:{
    type: Sequelize.DATE,
    field: 'Timestamp',
  },
  Identifier:{
    type: Sequelize.INTEGER,
    field: 'Identifier',
  },
  Building:{
    type: Sequelize.STRING,
    field: 'Building',
  },
  Device_Type:{
    type: Sequelize.STRING,
    field: 'Device_Type'
  },
  Unit:{
    type: Sequelize.STRING,
    field: 'Unit'
  },
  Energy:{
    type: Sequelize.DECIMAL,
    field: 'Energy'
  },
  Volume:{
    type: Sequelize.DECIMAL,
    field: 'Volume'
  },
  File_Name:{
    type: Sequelize.STRING,
    field: 'File_Name'
  }
},{
  timestamps: false,
  freezeTableName: true
});

Promise.all(mainDirectory.map(function(main) {
  return new Promise(function(resolve, reject) {
        fs.readdir(main, function(er,dirs){
            if (er) {
                return reject(er);
            }
            for(var i = 0; i < dirs.length; i++){
                dirs[i] = mainDirectory + "/" + dirs[i];
            }
            resolve(dirs);
        });
  }).then(function(dirs){
        return Promise.all(dirs.map(function(dir) {
          return new Promise(function(resolve, reject) {
            fs.readdir(dir, function(er, files) {
              if (er) {
                return reject(er);
              }
              resolve(files);
            });
          }).then(function(files) {
            return Promise.all(files.map(function(file) {
              return new Promise(function(resolve, reject) {
                fs.readFile(dir + '/' + file, 'utf-8', function(er, content) {
                  if (er) {
                    return reject(er);
                  }
                            var data_obj = Papa.parse(content,{
                                    quotes: false,
                                    delimiter: ";",
                                });
                                data_array = data_obj.data;
                                output_data_array = [];

                                for(var i = 10; i < data_array.length; i++){
                                    if(data_array[i][0] !== "" ){
                                        output_data_array.push({
                                                "Timestamp"     : data_array[i][0],
                                                "Identifier"    : data_array[i][1],
                                                "Building"      : file.split( "_" )[ 1 ],
                                                "Device_Type" : data_array[i][3],
                                                "Unit"          : data_array[i][5],
                                                "Energy"        : parseFloat(data_array[i][11].replace(',','.').replace(' ', '')) || 0 ,
                                                "Volume"    : parseFloat(data_array[i][13].replace(',','.').replace(' ', '')) || 0,
                                                "File_Name" : file
                                        });
                                    }
                                }
                        Relay.bulkCreate(output_data_array);
                    });
                resolve();
              });
            }));
          });
        }));
    });
})).catch(function(er) {
  console.log(er);
});



app.listen(process.env.PORT || 3000, process.env.IP, function(){
    console.log("Server is listening");
})

我可以通过执行以下操作来防止 ETIMEDOUT 错误:

setTimeout(function() {
            Relay.bulkCreate(output_data_array);
          }, 2);

我今天一直遇到这个问题,这对我有用:

let sequelize = new Sequelize(dbName, dbUsername, dbPassword, {
    host: dbHost,
    // the rest of your sequelize configuration...
    retry: {
        match: [
            /ETIMEDOUT/,
            /EHOSTUNREACH/,
            /ECONNRESET/,
            /ECONNREFUSED/,
            /ETIMEDOUT/,
            /ESOCKETTIMEDOUT/,
            /EHOSTUNREACH/,
            /EPIPE/,
            /EAI_AGAIN/,
            /SequelizeConnectionError/,
            /SequelizeConnectionRefusedError/,
            /SequelizeHostNotFoundError/,
            /SequelizeHostNotReachableError/,
            /SequelizeInvalidConnectionError/,
            /SequelizeConnectionTimedOutError/
        ],
        max: 5
    }
});