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

2019-09-21 10:26发布

问题:

My Nodejs Code:

i am trying to download files (excel) from a server and parse those excel files and from that data store only required data into mysql database i may download multiple files for that my loop may get interrupted so how to do my below code to run in a series of synchronous way

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

            }
          }
        }

回答1:

I (very quickly, so it probably has errors) rewrote this using async.forEachOfSeries to iterate over your attachments. I used async.forEachOf for the database writes as I don't see a need for them to be in series.

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();
            });
        }
    });
}