代码之家  ›  专栏  ›  技术社区  ›  João Gouveia

在Node JS中同步执行循环

  •  0
  • João Gouveia  · 技术社区  · 7 年前

    我使用Node JS向数据库发出请求。碰巧我正在执行一个SELECT语句,然后需要它的结果来执行INSERT。

     var index = 1;
        async.whilst(
            function() { return index < 10; },
            function(callback) {
                index++;
                let line = csv_json[index][0].split(';');
                let block = csv_json[index];
                read_account(line);
                callback(null, index);
            },
            function(err, n) {
                if (err) throw err;
            }
        );
    
    
    function read_account(csv, index) {
        if (index == csv_json.length) return;
    
        let line = csv_json[index][0].split(';');
        let block = csv_json[index];
        var account = line[0];
    
        waterfall([
            function(callback) {
                connection.query('SELECT id FROM account WHERE account_number = ' + account,
                    function(error, results, fields) {
                        if (error)
                            throw error;
                        console.log(results);
                        callback(null, results);
    
                    });
    
            },
            function(results, callback) {
    
                if (results == null || typeof results == undefined || results.length == 0) {
                    console.log("ENTREI");
                    connection.query('INSERT INTO account (account_number) VALUES (' + account + ')',
                        function(err, results, fields) {
    
                            callback(err, "1 account inserted!")
                            if (err)
                                throw err;
                        });
                }
                //else  callback(null,"record already in db")
    
            },
        ], function(err, success) {
            if (err) throw err;
            //console.log(success);
        });
    };
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sigma    7 年前

    function read_account(csv, index) {
        if (index == csv_json.length) return;
    
        let line = csv_json[index][0].split(';');
        let block = csv_json[index];
        var account = line[0];
        connection.query('SELECT id FROM account WHERE account_number = ' + account,
            function(error, results, fields) {
                if (error)
                    throw error;
                console.log(results);
    
                if (results == null || typeof results == undefined || results.length == 0) {
                    console.log("ENTREI");
                    connection.query('INSERT INTO account (account_number) VALUES (' + account + ')',
                        function(err, results, fields) {
    
                            callback(err, "1 account inserted!")
                            if (err)
                                throw err;
                        });
                }
    
            });
    };
    

    您可以考虑为select和insert编写两个不同的函数:

    function select(callback){
     connection.query('SELECT id FROM account WHERE account_number = ' + account, function(error, results, fields) {
          if (error) {
            return callback(error);
          } else {
            return callback(null, results, fields);
          }
      });
    }
    
    function insert(results, fileds, callback){
      connection.query('INSERT INTO account (account_number) VALUES (' + account + ')',
          function(err, results, fields) {
              if (err){
                return callback(err);
              } else {
                return callback(null, "1 account inserted!")
              }
          });
    }
    
    function read_account(csv, index, callback) {
        if (index == csv_json.length) return;
        let line = csv_json[index][0].split(';');
        let block = csv_json[index];
        var account = line[0];
    
        select(function(null, results, fields){
          if(err){
            return callback(err);
          } else {
            insert(results, fileds, callback);
          }
        });
      }