我有一个csv文件,我需要获取每一行的数据并将其转换为json并发送到服务器,所以我使用“fast-csv”来转换为Json和
还有另一个要求是,当用新数据更新文件时,我需要将文件中更新的新数据转换为json并发送到服务器,因此我使用尾流
当我使用它们来完成我的工作时,我正在使用下面的代码,但我正在
错误
data.pipe(csvStream); ^ TypeError:undefined不是函数
我的代码:
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);
});
答案 0 :(得分:1)
data
不是一个流。您无法在其上使用pipe()
。我想,正确的方法是:
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);