1

I try to read out a file with id's and return it as an array. Each row contains a single id. I would like to transfer each id to an array cell. That's my code:

var fs = require('fs');
var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
reader.once( 'end' ,  function ()  { 
    console.log( 'Read completed successfully.' ); 
});

var lineReader = require('readline').createInterface({
  input: reader,
});

function RTIdReader(){
  var arrayPos = 0;
  var idArray = new Array();

  lineReader.on('line', function (line) {
    console.log('Line from file:', line); 
    idArray[arrayPos] = line;
    arrayPos++;
  });

  console.log('idArray[0]: '+idArray[0]);
  return idArray;
}

RTIdReader();  

Do you have an idea, what I'm doing wrong? AND how would it be right?

@DrakaSAN: the code works until filling the array. I'm not able to console.log or return the array. The code stops after lineReader.on

I'm struggeling since round about two weeks with callbacks. And guess, I didn't understand them. That's my try:

var fs = require('fs');

function RT(idList){
  console.log('RT works');
}

function idList(){
  var idArray = new Array();
  var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
  reader.once( 'end' ,  function ()  { 
      console.log( 'Read completed successfully.' ); 
  });

  var lineReader = require('readline').createInterface({
    input: reader,
  });

  return {
    lineReader.on('line', function (line) {
      console.log('Line from file:', line); 
      idArray.push(line);
      return idArray;
    }
  }
}  

RT();

Why does my callback not work?

Okay, back to the beginning. That what I started with.

var fs = require('fs');

var idArray = new Array();
var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
reader.once( 'end' ,  function ()  { 
  console.log( 'Read completed successfully.' ); 
});

var lineReader = require('readline').createInterface({
  input: reader,
});

lineReader.on('line', function (line) {
  console.log('Line from file:', line); 
  idArray.push(line);
});

Where and how do I have to but the callback?

NewbieXXL
  • 155
  • 1
  • 1
  • 11
  • 1
    Is the code working? if not, what is it supposed to do/not do that it doesn t/do? – DrakaSAN Jun 17 '16 at 14:54
  • Instead of `idArray[arrayPos] = line; arrayPos++` you can simply do `idArray.push(line)`, which is more easily readable and less error prone. – DrakaSAN Jun 17 '16 at 14:55
  • 2
    It is a problem with asynchronous function, the console.log execute wayyyy before the file is read. – DrakaSAN Jun 17 '16 at 14:56
  • 1
    http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Jose Hermosilla Rodrigo Jun 17 '16 at 14:58
  • Thanks: I have tryed to put the return into function(line){...}, but then it returns the Array in the first round. Do you have an idea how I should write it? – NewbieXXL Jun 17 '16 at 14:58

1 Answers1

1

Use push when you add data to a array, the way you are doing it is adding property to a object in a array like fashion. Difference is thin, but may come to bite you in the ass later.

Check the readline documentation to learn how to use it, you do not need reader at all.

Your return take place at the same time than your code start to read the file, you need to check how asynchronous function works.

Tips for working code:

You don t need fs.createReadStream, at all.

The callback with all the ids in the array will be in a .on('close', function () {/*here*/});

EDIT:

In javascript, you can pass a function as a argument, and create anonymous function.

As such, you do not use return in async code, you pass the rest of the code as argument, which by convention is named callback.

As for how to use readline for reading a file, a fine example is in the documentation which can be converted for your use case in less than 5min.

EDIT:

Okay, i ve taken the time to make the code.

(arg) => {} is the same as function (arg) {}

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

function RT(file, callback) {
  let idArray = [];     //Declare idArray

  const rl = readline.createInterface({  //Set and start readline
    input: fs.createReadStream(file)
  });

  rl.on('line', (line) => { //For each line
    console.log('Line from file:', line);
    idArray.push(line);   //Add it to the array
  });

  rl.on('close', () => {  //At the end of the file
    callback(idArray);    //Call the rest of the code
  });
}

RT('RTIdList.js', (idArray) => {
  console.log('Here is the idArray: ' + idArray);
});
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • thanks, I tryed to work with callback. Please have a look, what's wrong with my code. – NewbieXXL Jun 17 '16 at 15:39
  • @NewbieXXL: No, you do not return in async code, go look at the link I provided, it can be easily edited to push the data in a array, then you only need to add a callback for it to work. – DrakaSAN Jun 17 '16 at 15:52
  • I have started with this example. But without a lot of experience I'm not able to convert this code for my wishes. – NewbieXXL Jun 17 '16 at 15:53
  • @NewbieXXL: I ve taken the time to give you the code, study it, try to understand what is going on, why and how it work, and compare to your code. You need to find a course about asynchronous programming and callback urgently. – DrakaSAN Jun 17 '16 at 15:59
  • Thank you very much, DrakaSAN. I will study the code! BTW What command is "=>"? I guess, I have figured out the answer. "=>" stands for function. Right? – NewbieXXL Jun 17 '16 at 16:07
  • `(arg) => {}` is the same as `function (arg) {}` in ES6, please read the entire answer/comment. – DrakaSAN Jun 17 '16 at 16:09