以下代码是一个解析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");
})
答案 0 :(得分:1)
我可以通过执行以下操作来阻止ETIMEDOUT错误:
val=(hi hello hi yes no hi good hi)
for i in $( seq 0 $( expr ${#val[@]} - 1 ) ) ; do [ ${val[${i}]} == "hi" ] && echo "'hi' is at position: ${i}"; done
答案 1 :(得分:0)
我今天一直在面对这个问题,这对我有用:
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
}
});