How to get the line number of a CSV file in function node

I would like to get the line number of a CSV file in function node.

I tried to execute the code below in function node but couldn't get the count value.
Do not know where the cause is?

[{"id":"61e780fd.ae573","type":"function","z":"e62775a6.462558","name":"","func":"var fs = global.get('fs');\nvar readline = global.get(\"readline\");\nvar filepath = \"/Temp/TestFile.CSV\";\n\n// line number of a CSV file\nvar count = 0;\n\nvar rd = readline.createInterface({\n    input: fs.createReadStream(filepath),\n    crlfDelay: Infinity\n});\n\nrd.on('line', function(line) {\n    count++;\n    node.warn(\"count Inside: \" + count);\n});\n\nnode.warn(\"■ ■ count OutSide: \" + count);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":1120,"wires":[["340867e7.bdde68"]]},{"id":"340867e7.bdde68","type":"debug","z":"e62775a6.462558","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":1120,"wires":[]},{"id":"9aa20a11.74a0a8","type":"inject","z":"e62775a6.462558","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":1120,"wires":[["61e780fd.ae573"]]}]

Where is readline coming from ? Is it a core module or the deprecated npm package ?

I declared it in the settings.js file:

functionGlobalContext: {
	fs:require('fs'),
	readline:require('readline')
}

I don't know, can use it after making the above declaration.

Hi yes it's fine. So you need to understand what your function is doing... when you see a line with .on it is only setting up a listener - it is not a loop. It just tells the system to listen for an event - in this case it is asking rd to tell it whenever it sees a line event. The function then proceeds to the next line and prints out the warning and the count that at that point is still 0. The readline process then tells you every time it gets a new line and the ".on" callback function you have there is called which adds to the count and prints that out...

What you also ned to do is listen for the close event that tells you when the file has finished reading - and then in that callback you can print out the final count.

I added the close event and still can't get the OutSide count
I don't understand what you say is a callback...

// line number of a CSV file
var count = 0;
var countA = 0;

var rd = readline.createInterface({
    input: fs.createReadStream(filepath),
    crlfDelay: Infinity,
    //output: count
});

rd.on('line', function(line) {
    count++;
    //node.warn("count Inside: " + count);
});

rd.on('close', () => {
    node.warn("count Inside: " + count); // Here got the final count OK
    countA = count;
    //context.set('AAA',count);
});

node.warn("■ ■ count OutSide: " + countA);
//node.warn("■ ■ count OutSide: " + context.get('AAA'));

You have to print the output from within the on close function.

The on functions are run asynchronously. That is to say that they are not run as part of the standard flow of your function node. They could happen at any time.

Imagine having a file containing 10 million lines. The on 'line' function would chug away for hours but the node-warn would be executed immediately.

Does this mean I with this approach cannot get the value of the variable count out of rd?

Nope. It means that you have to put a node.send inside the on close function and remove the return msg line from the end of your function node.

Getting used to Node.js's async processing takes some time. I recommend that you look for a tutorial.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.