如何在nodejs中使一个while循环成为一个系列

how to make a while loop in nodejs to be a series

我的 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);

            }
          }
        }

我(很快,所以它可能有错误)使用 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();
            });
        }
    });
}