05-13-2021 00:37
05-13-2021 00:37
I am trying to store the content of "console.log" to a test file in /private/datadata.
Since it override the prior data, I am about to use appendFile function to save previous contents.
But I meet "Unhandled exception: TypeError: Expected a function" when I use appendFile instead of writeFileSync('test.txt', "test", 'json').
I thought the error "Expected a function" requires me to wirte callback function, so I added it but still in the same situation.
Below is the code;
fs.appendFile('test.txt', 'test', 'json', function(err) {
if(err) { console.log(err);}
else { console.log("success");}
});
Answered! Go to the Best Answer.
05-13-2021 01:28
05-13-2021 01:28
appendFile isn't mentioned here; you need to stick to those functions.
05-13-2021 01:28
05-13-2021 01:28
appendFile isn't mentioned here; you need to stick to those functions.
05-13-2021 14:01
05-13-2021 14:01
Thank you for the answer.
So I tried with "openSync(filename, 'a+')" and "writeSync()".
But it creates separate files for each value named as 0, 1, 2, 3, 4.
Can I append them in one file?
05-13-2021 14:31
05-13-2021 14:31
I can't see why that wouldn't work (assuming you're not changing filename).
You may need to specify the 'offset' parameter in writeSync.
I actually used 'r+' to open a file for appending (when specifying offset).