0

So I have following code:

console.log('beforereadfile');

    fs.readFile(path.join(__static, '/client_secret.json'), 'utf8', function (err, data) {
        if (err) {
            console.log('Error loading client secret file: ' + err);

            return;
        }
        console.log('insidereadfile - data');
        authorize(JSON.parse(data));
    });

And it seems like the function that I provide to a async task readfile never triggers. I get 'beforereadfile' log but I neither get 'insidereadfile - data' nor an error message. Strange thing is that it seems to be working on a build version, but not on a development one. Any ideas why?

Tomek
  • 386
  • 1
  • 4
  • 11
  • Have you tried opening the file with readFileSync ? Does it work ? – Hugo Jan 24 '18 at 16:32
  • 1
    It worked now 5 times in a row but I'd rather stay with a async version if anybody has any idea – Tomek Jan 24 '18 at 16:36
  • Which version of node are you on ? Have you tried upgrading your node version – Hugo Jan 24 '18 at 16:40
  • I go on a 8.9.4 version which is to be said "recommended for most user" for now – Tomek Jan 24 '18 at 16:42
  • 1
    Is the process ending before the function completes? If the sync version works and the async version isn't then your process might be running to the end and shutting down before the asynchronous one calls the callback. – JamWils Jan 24 '18 at 17:03
  • what is the value of __static? is it meant to be __dirname? – kofman Jan 24 '18 at 17:10
  • @kofman __static is my own variable in the project – Tomek Jan 24 '18 at 17:22
  • @JamWils to be honest, I don't know how to check this, I thought that async calls create their own threads – Tomek Jan 24 '18 at 17:23
  • What happens if you put this `process.stdin.resume();` as the last line of code in your file? This will keep the process open as it waits for you to supply input from the terminal that is running node. I just tested your code and it works, so I think it's because the process is ending. – JamWils Jan 24 '18 at 18:29
  • @JamWils it works for me too on a build version, you want to put process.stdin.resume() after the readfile func? – Tomek Jan 24 '18 at 18:32
  • Yes, after the function, without knowing more about the environment you're running in this will keep the process open. Can you explain a little more about the build environment and how it differs from your dev environment? Again, it sounds like the node instance is exiting before the readFile function is completing. – JamWils Jan 24 '18 at 18:36
  • it's possible that if the authorize() call crashes at some point than the log does not get flushed to the console. See if replace authorize with just a printout of the data, and also try to surround the call with try/catch to catch a possible exception if thrown. The best would also be to debug and set a breakpoint inside the callback function. – kofman Jan 24 '18 at 18:58
  • @JamWils nothings happens when i put process.stdin.resume(), it goes to 'beforereadfile' log and then nothing, if you want to know more about the enviroment, maybe information that this is built with electron will be helpful – Tomek Jan 24 '18 at 23:08
  • @kofman I thought Im already catching errors with an error callback but I got this into a try catch block and got nothing, also I deleted authorize() callback inside and st ill nothing – Tomek Jan 24 '18 at 23:09
  • I thought maybe the problem is that I use this on a renderer process? – Tomek Jan 25 '18 at 12:43
  • @Tomek, here is a code snippet that I am using. I really can't help further without more information about the environment you are running in: https://gist.github.com/JamWils/9c08a8c2afbcc7db46cea31d5ee77c3f – JamWils Jan 25 '18 at 14:46

0 Answers0