05-13-2021 00:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-13-2021 00:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
Accepted Solutions
05-13-2021 01:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-13-2021 01:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
appendFile isn't mentioned here; you need to stick to those functions.
Gondwana Software

05-13-2021 01:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-13-2021 01:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
appendFile isn't mentioned here; you need to stick to those functions.
Gondwana Software

05-13-2021 14:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-13-2021 14:01
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-13-2021 14:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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).
Gondwana Software

