Get value of variable inside one function

97 views
Skip to first unread message

Felipe Silveira

unread,
Jan 17, 2014, 7:23:26 AM1/17/14
to nod...@googlegroups.com
Hi everyone,

I'm a newbie in nodejs and express, so i'm trying to generate one CSV file reading a content of one log file, something like this:

  var csv;
  var instream = fs.createReadStream(pathLog+req.params.file);
  var outstream = new stream;
  outstream.readable = true;
  outstream.writable = true;
  var rl = readline.createInterface({
      input: instream,
      output: outstream,
      terminal: false
  });

  rl.on('line', function(lin,csv) {
    var csv;
      if(req.params.id != ''){
        var re = new RegExp(req.params.id, 'i');
        
        if(lin.match(re) != null){
          var string = lin.substring(143,lin.length);
          csv += ','+string;
        }
      }
  });
  res.set('Content-Type', 'application/octet-stream');
  console.log(csv);


The real problem is, I declared the variable csv and in the event line I'm incrementing that but in the console.log return undefined.

Thank you!


Att,
Felipe Silveira Mendes
Site - Twitter - Blog
Web Developer
(31) 8370-9090 (Claro)
(31) 9433-9310 (Tim)


willem dhaeseleer

unread,
Jan 17, 2014, 8:20:08 AM1/17/14
to nod...@googlegroups.com
The first problem is that you are not waiting for the stream to finish. Your console.log is called immediately.
You're also shadowing the csv variable in your line event, so you need to remove the local csv variable there as well.


On Friday, January 17, 2014 1:23:26 PM UTC+1, Felipe Silveira wrote:
Hi everyone,

I'm a newbie in nodejs and express, so i'm trying to generate one CSV file reading a content of one log file, something like this:

  var csv;
  var instream = fs.createReadStream(pathLog+req.params.file);
  var outstream = new stream;
  outstream.readable = true;
  outstream.writable = true;
  var rl = readline.createInterface({
      input: instream,
      output: outstream,
      terminal: false
  });

  rl.on('line', function(lin,csv) {
    var csv; // this one is shadowing the one from the parent scope
      if(req.params.id != ''){
        var re = new RegExp(req.params.id, 'i');
        
        if(lin.match(re) != null){
          var string = lin.substring(143,lin.length);
          csv += ','+string;
        }
      }
  });
  res.set('Content-Type', 'application/octet-stream');
  console.log(csv); // You need to wait for the readline close event before logging out

Felipe Silveira

unread,
Jan 17, 2014, 10:07:56 AM1/17/14
to nod...@googlegroups.com
Willem,

Thank you for help, sorry but I'm beginner and still do not understand some things, like how I'll wait for the event line finish and so run the console.log?
I changed the code to this, its correct?

  var csv;
  var instream = fs.createReadStream(pathLog+req.params.file);
  var outstream = new stream;
  outstream.readable = true;
  outstream.writable = true;
  var rl = readline.createInterface({
      input: instream,
      output: outstream,
      terminal: false
  });

  rl.on('line', function(lin) {
    if(req.params.id != ''){
        var re = new RegExp(req.params.id, 'i');
        if(lin.match(re) != null){
          var string = lin.substring(143,lin.length);
          csv += ','+string;
        }
      }
  });
  res.set('Content-Type', 'application/octet-stream');
  //console.log(csv);
  //res.send(exports.csv);
});

willem dhaeseleer

unread,
Jan 17, 2014, 11:36:34 AM1/17/14
to nod...@googlegroups.com
Like documented here:

You need to wait for the close event, in just the same way you wait for the 'line' event.

Of Course i can't guarantee if this is going to work. I'm not sure if your code inside your line handler is going to be any good.
I added a console.log there as well so that you can see what gets added, but you should remove that once everything works.

And of course, instead of building up your csv file into 1 variable, you should write through to an actual output stream so that your memory doesn't skyrocket. Taking that into account I'm not sure if readline is the best way to do this, since it will might be cumbersome to integrate it into a backpressured pipeline. I think this is because readline is more for terminal interaction than just trivial line splitting. ( I could be wrong on that, anyone ?)


On 17 January 2014 16:07, Felipe Silveira <con...@felipems.com.br> wrote:
Willem,

Thank you for help, sorry but I'm beginner and still do not understand some things, like how I'll wait for the event line finish and so run the console.log?
I changed the code to this, its correct?

  var csv;
  var instream = fs.createReadStream(pathLog+req.params.file);
  var outstream = new stream;
  outstream.readable = true;
  outstream.writable = true;
  var rl = readline.createInterface({
      input: instream,
      output: outstream,
      terminal: false
  });

  rl.on('line', function(lin) {
    if(req.params.id != ''){
        var re = new RegExp(req.params.id, 'i');
        if(lin.match(re) != null){
          var string = lin.substring(143,lin.length);
              console.log('Adding Line', string); 
          csv += ','+string;
        }
      }
  });
  res.set('Content-Type', 'application/octet-stream');
 
      rl.on('close', function() {
            console.log('TOTAL CSV', csv);
      })
 

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/qQ6prse3r0c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Felipe Silveira

unread,
Jan 17, 2014, 11:54:28 AM1/17/14
to nod...@googlegroups.com
Thank you more one time, worked fine.
Do you have examples the better way to generate the csv like you told using output stream?

greelgorke

unread,
Jan 20, 2014, 3:30:37 AM1/20/14
to nod...@googlegroups.com
probably you should use https://github.com/dominictarr/split instead of readline, because readline is designed with cli-dialogs in mind. with split your application could be like this (example):

var instream = fs.createReadStream(pathLog+req.params.file);
var trans = new require('stream').Transform({objectMode: true})
trans._transform = function( chunk, encoding, callback){
   // do something here with the chunk, which is a line
   // when done, push it
   this.push(chunk)
   callback()
}

//outstream can be another file or the response object
 instream.pipe(split).pipe(trans).pipe(outstream)
Reply all
Reply to author
Forward
0 new messages