0

As a temporary method, I use a .txt file to store certain variables of my program. Writing goes perfectly with fs.appendFile, but given the size of it, reading with fs.readFile is not suitable - I'd like to get a certain line from the file, and npm line-by-line was told me may help.

I'm a bit lost with it, though. This is a function I call:

function LBL_SetValueToLine(path, lineid, value){

    var lr = new lblreader(path);
    var m = 0;

    lr.on('line', function (line) {
        // 'line' contains the current line without the trailing newline character.
        m=+1;
        if(m==lineid){ value = line; };
    });

};

And the call itself happens as a usual function call, with all input variable being surely proper.

I should have thought this is not a proper method, but the documentation said, this is the synchronous method. However, I can see that the work of .on is asysnc, because of the function input it needs.

Not sure if it has anything to do with the problem, but anyways, console.log after the function call always indicated that the variable hasn't been changed.

How can I do it?

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48
  • Not sure I understand your question, but if you are asking for how to make this synchronous then [that's impossible](http://stackoverflow.com/q/14220321/1048572?how-to-return-the-response-from-an-asynchronous-call). – Bergi Mar 23 '16 at 00:39

2 Answers2

3

You can use the Readline module that is provided by the Core Node API.

Readline API Example for Line-by-Line

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

const rl = readline.createInterface({
  input: fs.createReadStream('sample.txt')
});

rl.on('line', (line) => {
  console.log('Line from file:', line);
});

The example above was taken from the Node Readline API Docs

peteb
  • 18,552
  • 9
  • 50
  • 62
  • At first, I was scared, because my first assumption was that `rl.on` needs some complex asnyc way to call - I should have thought it over when I saw that `line-by-line` does the same way, but anyways, I have to ask: **does it needs async call?** Does it go on with executing the code after establishing `rl.on`, or it waits until it is "closed"? – Zoltán Schmidt Mar 23 '16 at 00:34
  • Yes, this doesn't block your code after `rl.on()` is set to listen. There is the `close` event which essentially is the equivalent of an async callback. If you need more control flow you'll need to implement some other library to wrap this in like `bluebird` or `async`. – peteb Mar 23 '16 at 00:46
  • Ah, great...do I think well, that a bigger NodeJS app is full of these **callback within the callback of my callback** situations? – Zoltán Schmidt Mar 23 '16 at 01:24
  • @ZoltánSchmidt I'm not sure I understand what your last comment is trying to say – peteb Mar 23 '16 at 01:28
  • It's a bit hard to put into other context, though. The matter is that I'm implementing `rl.on` and this is the third callback I write. First it was a jQuery AJAX request. Within its callback there's Readline, and within its callback another HTTP request is coming. So, it's quite nested. In the past, I thought it's rather rare in NodeJS. – Zoltán Schmidt Mar 23 '16 at 01:32
  • Anyways, I was successful with ReadLine, so thanks for your answer. – Zoltán Schmidt Mar 23 '16 at 03:16
1

The variable didn't change because the videoid was passed in by value. This means that any change to it within the function is not reflected on the variable itself.

For example:

function increment(num) {
  num++;
}
var a = 1;
increment(a);
console.log(a); // 1

To pass in a variable that would be changed by the function, you'll need to pass in an object type.

For example:

function increment(num) {
  num.val++;
}
var a = {val: 1};
increment(a);
console.log(a); // Object {val: 2}

To apply this to your program, you'll need to create an object to hold the videoid as well as close the stream when you're done reading:

var videodata = {videoid: 'someID'};
function YTAPP_LBL_SetValueToLine(path, lineid, viddata){

  var lr = new lblreader(path);
  var m = 0;

  lr.on('line', function (line) {
    // 'line' contains the current line without the trailing newline character.
    m += 1;
    if(m == lineid){ 
      viddata.videoid = line;

      // Close the stream
      lr.close();
    }
  });



};
gnerkus
  • 11,357
  • 6
  • 47
  • 71