fs.writeFile() doesn't return callback
Asked Answered
S

3

8

I'm trying to write a file with the users authentication data to the disk. To achieve this I wrote the following function:

function writeAuthFile(data, success, fail) {
  var fs = require('fs');
  fs.writeFile('auth.json', JSON.stringify(data), function(error) {
    if(error) { 
      console.log('[write auth]: ' + err);
        if (fail)
          fail(error);
    } else {
      console.log('[write auth]: success');
        if (success)
          success();
    }
  });
}

But it never calls the callback. I looked at the nodeJS docs for fs and it all seems to check out. Also all other asynchronous execution seems to have halted.

This is the first time I'm developing something serious in nodeJS so my experience in this environment is not that much.

Sciurine answered 16/2, 2015 at 15:40 Comment(2)
Just to be perfectly clear: by "never calls the callback" do you mean that your console.log calls never run, or that the success/fail callbacks don't run?Cyd
Both and all the execution of other calls seems to have halted too.Sciurine
I
2

Your code looks fine, I copy&paste and run it by simply calling writeAuthFile({test: 1});, file auth.json was created. So, mb error somewhere higher? add console.log after var fs = require('fs'); line and test.

Irrelevance answered 16/2, 2015 at 15:56 Comment(5)
I had a console.log() after var fs = require('fs'); and it worked just fine. Also everything seems to halt. After the writeAuthFile() call I do all sorts of other stuff, that now doesn't happen. Maybe it has something to do with permissions or maybe the creation of a file handle. The docs say that it should go to the callback and populate the error parameter.Sciurine
Ok, check your data, mb it contains something wrong for JSON.stringify?Irrelevance
Thank you very much! It seems to be the JSON.stringify() call. Which is very weird, the object has been created from a JSON string before through the JSON.parse() function.Sciurine
I'm seeing a similar behaviour in my code, so wanna ask: was there an error in data that the JSON.stringify() call didn't work or it didn't work with or without correct data being passed? At my end, the write to file works if I use fs.writeFileSync(file, data), i.e synchronous method but I don't like it already.Fortyfour
having the same issue, the callback never gets called sometimesMilfordmilhaud
F
0

the only thing i see thats off, is that if it fails it tries to log "err" instead of "error"

if(error) {
    console.log('[write auth]: ' + err); //<-this should be "error" and not "err"
Fiddlehead answered 27/5, 2018 at 21:14 Comment(0)
O
0

there is no param callback if file already exist. and the promise is resolved with no argument upon success. if you wanna check file already exist use another function of filehandle then writefile.enter image description here enter image description here

Orcus answered 12/3, 2021 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.