I have the following node.js code:
new lazy(fs.createReadStream('file.csv'))
.lines
.forEach(function(line){
console.log(line.toString());
});
However, I only get the last line of data this way. The contents of the csv file are as follows:
123,broken
12345,stolen
1234567,lost
What am I doing wrong here?
I also have this code for the same file:
fs.readFile(req.files.file.path, 'utf8', function (err, data) {
if (err) throw err;
var lines = data.split(/\r?\n/);
console.log(lines);
});
Which returns the following array:
[ '123,broken\r12345,stolen\r1234567,lost' ]
\r\n
, \n
, and \r
are all valid line endings, so you need to be prepared to split on all of them.
Convert them all to a common value before doing the split. Something like:
var lines = data.replace(/\r\n?/g, "\n").split("\n");