nodejs - csv-write-stream在stdout的回调中给了'pipe不是函数'

时间:2017-06-25 15:32:47

标签: javascript node.js csv

每当我运行此代码时,我都会收到一条错误消息csv.pipe is not a function

我认为可能与stdout中的回调有关?

var fs = require('fs');
var spawn = require('child_process').spawn;
var speedtest = spawn('speedtest-cli',['--json']);
var csv = require('csv-write-stream')

speedtest.stdout.on('data', function(data){
    var dataObj = JSON.parse(data);
    var down = (dataObj.download/1000000).toFixed(2);
    var up = (dataObj.upload/1000000).toFixed(2);

    csv.pipe(fs.createWriteStream('speed.csv',{flags: 'a'}));
    csv.write([dataObj.timestamp,down,up]);
    csv.end();
});

1 个答案:

答案 0 :(得分:0)

var fs = require('fs');
var spawn = require('child_process').spawn;
var speedtest = spawn('speedtest-cli',['--json']);
var csvWriteStream = require('csv-write-stream');
var csv = csvWriteStream();//I added this string

speedtest.stdout.on('data', function(data){
    var dataObj = JSON.parse(data);
    var down = (dataObj.download/1000000).toFixed(2);
    var up = (dataObj.upload/1000000).toFixed(2);

    csv.pipe(fs.createWriteStream('speed.csv',{flags: 'a'}));
    csv.write([dataObj.timestamp,down,up]);
    csv.end();
});