我的Nodejs代码:
我正在尝试从服务器下载文件(excel)并解析那些excel文件,并从那个数据存储中只将所需数据存入mysql数据库我可能会下载多个文件,因为我的循环可能会被中断所以如何执行我的下面的代码以一系列同步方式运行
var r=0;
if (issue.fields.attachment != '') {
while (typeof issue.fields.attachment[r] != "undefined") {
if (typeof issue.fields.attachment[r].content != "undefined") {
var url = issue.fields.attachment[r].content;
request({
method: "GET",
"url": url,
"headers": { "Content-Type": "application/json", }
}, function(err, data, body) {
console.log('file downloading');
}).pipe(fs.createWriteStream('file.xlsx'));
console.log('file downloaded');
parseXlsx('file.xlsx', function(err, data) {
var i, j, k = 1, l = 0, m = 0, n = 0;
console.log('parseXlsx cmpleted');
while (data[i] != undefined) {
if (data[i][j] != '' || data[i][k] != '' || data[i][l] != '') {
var query = connection.query('insert into IP values ("' + data[i][j] + '","' + data[i][k] + '","' + data[i][l] + '","' + data[i][m] + '")',
function(error, results, fields) {
});
}
i++;
}
});
r++;
console.log('rvalue' + r);
}
}
}
答案 0 :(得分:1)
我(非常快,所以它可能有错误)使用async.forEachOfSeries
重写了这个以迭代您的附件。我使用async.forEachOf
进行数据库写入,因为我认为不需要将它们串联起来。
var async = require('async');
if (issue.fields.attachment != '') {
async.forEachOfSeries(issue.fields.attachment,function(attachment,r,callback){
if (typeof issue.fields.attachment[r].content != "undefined") {
var url = issue.fields.attachment[r].content;
var ws = fs.createWriteStream('file.xlsx');
request({ method: "GET", "url": url, "headers": { "Content-Type": "application/json" }
}, function(err, data, body) {
console.log('file downloading');
}).pipe(ws);
ws.on('finish',function() {
console.log('file downloaded');
parseXlsx('file.xlsx', function (err, data) {
var i, j, k = 1, l = 0, m = 0, n = 0;
for (i = 0; i < 15; i++) {
for (j = 0; j < 15; j++) {
if (regex.test(data[i][j])) {
k = 0;
break;
}
}
if (k == 0)
break;
}
for (k = 0; k < 15; k++) {
if (regex1.test(data[i][k])) {
break;
}
}
for (l = 0; l < 15; l++) {
if (regex2.test(data[i][l])) {
break;
}
}
for (m = 0; m < 15; m++) {
if (regex2.test(data[i][m])) {
break;
}
}
i = i + 1;
console.log('parseXlsx completed');
async.forEachOf(data,function(row,i,_callback){
if(i===0)return _callback();
if (data[i][j] != '' || data[i][k] != '' || data[i][l] != '') {
var query = connection.query('insert into IP values ("' + data[i][j] + '","' + data[i][k] + '","' + data[i][l] + '","' + data[i][m] + '")',
function (error, results, fields) {
if(error){
//Decide if you want to stop writing rows if one insert fails, if so uncomment next line
//return _callback(error);
}
return _callback();
});
}
},function(err){
if(err){
//decide if you want to stop the whole process if the database errored, if so, uncomment next line
//return callback(err);
}
callback();
});
});
});
ws.on('error',function(err) {
//There was an error writing the file to disk, but I assume you want to continue anyway so I'm calling callback()
//If you want to stop processing, call callback(new Error('Failed writing to disk'));
return callback();
});
}
});
}