如何使nodejs中的while循环成为一个系列 [英] how to make a while loop in nodejs to be a series

查看:245
本文介绍了如何使nodejs中的while循环成为一个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Nodejs代码:

My Nodejs Code:

我正在尝试从服务器下载文件(excel),并解析这些excel文件,并从该数据存储只需要数据到mysql数据库我可以下载多个文件,我的循环可能会中断,所以如何做我的下面的代码以一系列同步方式运行

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

            }
          }
        }


推荐答案

我(很快,所以它可能有错误)重写了这个使用 async.forEachOfSeries 来迭代你的附件。我使用 async.forEachOf 作为数据库写入,因为我没有看到他们需要串行。

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

这篇关于如何使nodejs中的while循环成为一个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆