Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

When appendFile, Unhandled exception: TypeError: Expected a function keep

ANSWERED

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");}
});

Best Answer
1 BEST ANSWER

Accepted Solutions

appendFile isn't mentioned here; you need to stick to those functions.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
0 Votes
3 REPLIES 3

appendFile isn't mentioned here; you need to stick to those functions.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

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?

Best Answer
0 Votes

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).

Peter McLennan
Gondwana Software
Best Answer
0 Votes