Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statements order in Node js

Tags:

node.js

new to Node JS, still getting used to the way things are done. I am processing a file with this structure:

[1447113604] [104] [86]
[1447113605] [111] [91]
[1447113606] [127] [114]
[1447113607] [115] [105]
[1447113608] [120] [101]
[1447113609] [105] [100]
[1447113610] [89] [88]

I read it line by line,parse each line and create and object that has 3 properties (timestamp, count1, count2), then I add the newly created object to an array, at the end I want to loop through the array and print its elements with a for loop.

var readline = require('readline');
var fs = require('fs');

var arr_counters = [];

var rl = readline.createInterface({        
      input : fs.createReadStream("cps.log"),
      output: process.stdout,
      terminal: false    
})

rl.on('line',function(line){
    console.log(line) //or parse line
    var tokens = line.split(" ");

  var str_TS = tokens[0].substring(1,11);   //strip '[' ']'
  var int_p = tokens[1].substring(1, tokens[1].length - 1); //strip '[' ']'
  var int_c = tokens[2].substring(1, tokens[2].length - 1);//strip '[' ']'

    console.log(str_TS.toString());
    console.log(int_p.toString());
    console.log(int_c.toString());

    var counter = {
        timestamp: parseInt(str_TS),
        PCount: parseInt(int_p),
        CCount: parseInt(int_c)
    }
    arr_counters.push(counter);
})

for (var index = 0; index < arr_counters.length; index++)
    console.log(arr_counters[index]);

But the last 2 lines seemed to be skipped, I can't see the elements of the array: This is the current output:

E:\Node-JS>node read_file2.js
[1447113604] [104] [86]
1447113604
104
86
[1447113605] [111] [91]
1447113605
111
91
[1447113606] [127] [114]
1447113606
127
114
[1447113607] [115] [105]
1447113607
115
105
[1447113608] [120] [101]
1447113608
120
101
[1447113609] [105] [100]
1447113609
105
100

If I place the for loop right after pushing the object in the array then I see them as they are added on each iteration, the question is how can I access the array properly after it has been filled with the objects ?

like image 204
user3196371 Avatar asked Apr 27 '26 20:04

user3196371


1 Answers

The issue is that your loop is running before your readLine is finished parsing. You need to call your loop only once the entire file is parsed via its close event.

var readline = require('readline');
var fs = require('fs');

var arr_counters = [];

var rl = readline.createInterface({        
      input : fs.createReadStream("cps.log"),
      output: process.stdout,
      terminal: false    
})

rl.on('line',function(line){
    console.log(line) //or parse line
    var tokens = line.split(" ");

  var str_TS = tokens[0].substring(1,11);   //strip '[' ']'
  var int_p = tokens[1].substring(1, tokens[1].length - 1); //strip '[' ']'
  var int_c = tokens[2].substring(1, tokens[2].length - 1);//strip '[' ']'

    console.log(str_TS.toString());
    console.log(int_p.toString());
    console.log(int_c.toString());

    var counter = {
        timestamp: parseInt(str_TS),
        PCount: parseInt(int_p),
        CCount: parseInt(int_c)
    }
    arr_counters.push(counter);
})


rl.on('close',function(){
    for (var index = 0; index < arr_counters.length; index++)
        console.log(arr_counters[index]);
});
like image 72
Yuri Zarubin Avatar answered Apr 30 '26 11:04

Yuri Zarubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!