FabioEnne FabioEnne - 2 months ago 16
Javascript Question

How to keep on writing on the same csv file?

I'm using this nodejs package in order to write some data to a CSV file every minute, so far the creation of the file is ok, also the first time write is ok, but when it comes the time to write again on the same file nodejs throws me error, I tried to look around for a solution but I came up whit nothing, hope someone could help me out. I'll post what I have right now

var cronCSV = require('node-schedule');
var csvWriter = require('csv-write-stream');
var writer = csvWriter({sendHeaders: false});
var ruleCSV = new cron.RecurrenceRule();
ruleCSV.second = 1;
const fs = require('fs');

cronCSV.scheduleJob(ruleCSV, function(){

writer.write({hPerc: hPerc, humidity: humidity});
console.log("Scritto su CSV");

And the error:

dest.on('unpipe', onunpipe);

TypeError: Cannot read property 'on' of undefined
at CsvWriteStream.Readable.pipe (_stream_readable.js:501:7)
at Job.job (/home/pi/node_led/main.js:64:10)
at Job.invoke (/home/pi/node_led/node_modules/node-schedule/lib/schedule.js:175:10)
at Timeout._onTimeout (/home/pi/node_led/node_modules/node-schedule/lib/schedule.js:479:11)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)


You are closing the pipe just after writing to it first (and each) time:

You should remove this row:


If the process is intended to finish any time, then it would be a good idea to execute it just before that. But not when you expect to write to it more times.