0

What I'm trying to do is to download a csv file, read it line by line and to add the splitted line (on ',') to tmparray. This code works and prints all the element in the array.

var request = require('request');
var fs = require('fs');
readline = require('readline');
try {
    request('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.csv').pipe(fs.createWriteStream("MyCsv.txt"));
} catch (e) {
    console.error(e);
}
var inputFile = 'MyCsv.csv';
var tmparray;
//read the file
var rd = readline.createInterface({
    input: fs.createReadStream('/home/nome/Node/MyCsv.csv')
});
try {
    //read line by line 
    rd.on('line', (line) => {
        tmparray += line.split(",");

        //print the elements
        tmparray.forEach((element) => {
            console.log(element);
        }, this);
    });
} catch (e) {
    console.error(e);
}

What I want to do is to print the array after I assigned it. I've tried this:

var request = require('request');
var fs = require('fs');
readline = require('readline');
try {
    request('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.csv').pipe(fs.createWriteStream("MyCsv.txt"));
} catch (e) {
    console.error(e);
}
var inputFile = 'MyCsv.csv';
var tmparray;
//read the file
var rd = readline.createInterface({
    input: fs.createReadStream('/home/nome/Node/MyCsv.csv')
});
try {
    //read line by line 
    rd.on('line', (line) => {
        tmparray += line.split(",");


    });
} catch (e) {
    console.error(e);
} finally {
    console.log(tmparray); // undefined
    // or this: console.log(tmparray[0]) can't read the property '0' of undefined

}

but the array is printed as if it is undefined

2 Answers 2

2

The problem is that rd.on(...) is asynchronous.

That means that you are telling rd that when it reads a line, it should add it to tmparray — but that doesn't actually happen yet. It happens moments later, after you console.log(tmparray).

You should say rd.on('close', () => console.log(tmparray)) to tell Node "when you have finished reading rd, then log the data".

There are a couple of other issues in the code but they should be easier to find once this is fixed. Looking at it, I think line isn't an event on readable streams so you should say rd.on('data', ...) instead; and you're trying to build up an array using the + operator which doesn't work. It will probably convert everything to strings though, so it should log something fairly reasonable for now.

Sign up to request clarification or add additional context in comments.

1 Comment

Will try tomorrow. Makes sense
2

Why not use the csv package it will give you the same result, Here is an example of transforming csv file into array:

const csv = require('csv')
    , request = require('request');

var url = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.csv';

request(url, function (err, response, data) {

    if (err) throw err;

    csv.parse(data, function (err, data) {

        if (err) throw err;

        // here you get your array
        console.log(data);

    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.