How can you append text to an existing file during periodicactivity in Node.js? My goal is to log successive sensor values into a textfile.
I am able to create a file and write a single value to it during periodic activity, but each time I append that file the old data is erased (or perhaps a new file is created altogether). Any data that was in the file prior to periodicactivity() is also lost.
fs.open('SensorData.txt', 'w', function(err, data) {
if (err) {
console.log("ERROR !! " + err);
} else {
fs.appendFile('SensorData.txt', content, function(err) {
if (err)
console.log("ERROR !! " + err);
fs.close(data, function() {
console.log('written success');
})
});
}
I found the above code on stack exchange, but I am unable to make it function in this context. I'm new to Node.js and I'm sure there is a better way to accomplish my task. I would greatly appreciate any input.
Thank you jfriend00!