简体   繁体   中英

Node.js need to wait for for loop to finish before executing function

Ok. So i'm extremely new to node. my mind is spinning in callback hell right now. i've read through many similar questions but can't seem to get it to run properly.

essentially, i'm parsing through server logs regex'ing for specific strings that i'm going to use to run some sql queries with. I've gotten this to work on a smaller scale where a loop is not needed.

I need to wait for all of the ppsid's to run through _cssPPSID, which is generating a dictionary object and my query filter, before running a query using that filter in the query itself and then matching against that dictionary to pair the results with the data parsed from the server logs.

As of right now this code runs, but it stops after the first iteration. I'm presuming i'm missing a callback or something simple so that the script can get back to the loop.

var ppsDict = new Object();
var ppsList = new Array();

var getURL = function(stdout) {
    console.log('Creating ppsid url key value pairs');
    var url_reg = /http\S+/g;
    var ppsid_reg = /PPS_ID\=[0-9]+/g;
    var ppsid = stdout.toString().match(ppsid_reg);
    var url = stdout.toString().match(url_reg);
    ppsDict[ppsid[0]] = url[0];
    ppsList.push(ppsid[0]);
    console.log(ppsDict);
  };


var getPPSID = function(stdout, prodServer, searchResultProcessed) {
    console.log("Collecting PPSID's");
    var ppsid_reg = /PPS_ID\=[0-9]+/g;
    var selector_reg = /selector\s\S+/g;
    var ppsid = stdout.toString().match(ppsid_reg);
    var selector = stdout.toString().match(selector_reg);
    async.eachSeries(ppsid, function (ppsSelector, next) {
      var urlQ = new urlQuery(prodServer, searchResultProcessed,  ppsSelector);
      _cssPPSID(urlQ, function(err, result) {
         if(err) {
          console.log(err);
        }
        next();
      }) 
    }, function (err) {
        console.log('iterating done');
        //queryProd(ppsList,ppsDict);
    });
}


function urlQuery(prodServer, searchResultProcessed, ppsSelector) {
  this.cssPPSID = "ssh " + prodServer + " 'sudo cat  /mint/logs/topologies/'" + searchResultProcessed + " | grep " + ppsSelector  + " | grep \"Downloading\"";
}

var _cssPPSID = (urlQ) => {
  child_process.exec(urlQ.cssPPSID, ["-m"], (err, stdout, stderr) => {
    if (stdout === 0) {
      console.log('PPS_ID and CSS error combination not found');
      return;
    } else if (err) {
     console.error(err, stderr);
      return;
    }
    getURL(stdout);
  });
};

Your async.eachSeries won't work because function _cssPPSID doesn't take a callback as second argument, so the next() is never called.

Try rewriting it like this:

var _cssPPSID = (urlQ, callback) => { 
  child_process.exec(urlQ.cssPPSID, ["-m"], (err, stdout, stderr) => {
    if (err) {
      return callback(err);
    }        
    if (stdout === 0) {
      return callback(new Error('PPS_ID and CSS error combination not found');
    }
    // do stuff here
    callback(null); // null indicates no error has happened
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM