Labeo
Labeo

Reputation: 6349

How to use csv data given from tail-stream to convert to Json in Nodejs

I am having a csv file where i need to get data of each row and conver it to json and send to a server so i am using "fast-csv"cfor converting to Json and

there is another requirement that when ever the file is updated with new data i need to convert the newdata updated in the file to json and send to server so i am using tail-stream

when i use both of them to get my work done i am using below code but i am getting

error

data.pipe(csvStream); ^ TypeError: undefined is not a function

My code:

var ts = require('tail-stream');
 var csv = require("fast-csv");
var tstream = ts.createReadStream('test.csv', {
    beginAt: 0,
    onMove: 'follow',
    detectTruncate: false,
    onTruncate: 'end',
    endOnError: false
});

tstream.on('data', function(data) {
    //console.log("got data: " + data);
    console.log('inside\n');

    var csvStream = csv()
    .on("data", function(data){
         console.log(data);
    })
    .on("end", function(){
         console.log("done");
    });

            data.pipe(csvStream);

});

tstream.on('eof', function() {
    console.log("reached end of file");
});

tstream.on('move', function(oldpath, newpath) {
    console.log("file moved from: " + oldpath + " to " + newpath);
});

tstream.on('truncate', function(newsize, oldsize) {
    console.log("file truncated from: " + oldsize + " to " + newsize);
});

tstream.on('end', function() {
    console.log("ended");
});

tstream.on('error', function(err) {
    console.log("error: " + err); 
});

Upvotes: 0

Views: 293

Answers (1)

throrin19
throrin19

Reputation: 18197

data is not a stream. You can't use pipe() on it. I think, the correct way is this :

var ts = require('tail-stream');
var csv = require("fast-csv");
var tstream = ts.createReadStream('test.csv', {
    beginAt: 0,
    onMove: 'follow',
    detectTruncate: false,
    onTruncate: 'end',
    endOnError: false
});
var csvStream = csv()
.on("data", function(data){
     console.log(data);
})
.on("end", function(){
     console.log("done");
});
tstream.on('eof', function() {
    console.log("reached end of file");
});
tstream.on('move', function(oldpath, newpath) {
    console.log("file moved from: " + oldpath + " to " + newpath);
});
tstream.on('truncate', function(newsize, oldsize) {
    console.log("file truncated from: " + oldsize + " to " + newsize);
});

tstream.pipe(csvStream);

Upvotes: 1

Related Questions