node-csv-stream icon indicating copy to clipboard operation
node-csv-stream copied to clipboard

emit end event is not working as expected

Open sridhargoshika opened this issue 6 years ago • 0 comments

I am trying to terminate the parsing and emit the end event. Here is my code, any help? var csvStream = csv.createStream({ escapeChar: '"', enclosedChar: '"' });

let csvData = {headers: {},rows:[]};
request(req.body.fileUrl).pipe(csvStream)
    .on('error', function (err) {
        console.error(err);            
    })
    .on('header', function (columns) {
        csvData.headers = columns;
        console.log(columns);
    })
    .on('data', async function (data) {
        console.log(data);
        csvData.rows.push(data);
        if (csvData.rows.length >= 100) { 
            csvStream.emit('end'); // I need only top 100 rows
        }
    })
    .on('end', async function () {           
        // run the last of the operations            
        csvData.rows = csvData.rows.slice(0, 10);
        return res.send(csvData);
    })
    .on('column', function (key, value) {
        // outputs the column name associated with the value found
        console.log('#' + key + ' = ' + value);
    });`

sridhargoshika avatar Oct 21 '19 06:10 sridhargoshika