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.

Feanaro
  • 922
  • 3
  • 19
  • 35
  • 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? – apsillers Feb 16 '15 at 15:54
  • Both and all the execution of other calls seems to have halted too. – Feanaro Feb 16 '15 at 16:00

3 Answers3

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.

Daniel
  • 364
  • 2
  • 8
  • 1
    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. – Feanaro Feb 16 '15 at 15:59
  • Ok, check your `data`, mb it contains something wrong for `JSON.stringify`? – Daniel Feb 16 '15 at 16:04
  • 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. – Feanaro Feb 16 '15 at 16:07
  • 1
    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. – Marvin Danig Jun 07 '16 at 00:09
  • 1
    having the same issue, the callback never gets called sometimes – Sohail Faruqui Sep 28 '17 at 11:26
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"
LightWing
  • 88
  • 7
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