0

Im having a strange issue in my NodeJS app. Im using

function getFile(filename, cb){
    fs.readFile(filename, 'utf8', (err, data) => {
        if(err){
            throw err;
            return;
        }
        return cb(data);
    });
}

to open an html file, get its contents and send to the client for different modal screens. However, the first html file it opens seems to be getting cached, somehow. I know there's no real caching in the nodejs fs, so Im unsure as to where the problem could be stemming from. Ive ensured that the correct values are in fact being passed into the function, but the output is always the first file opened. How can I stop this from happening?

chaoskreator
  • 889
  • 1
  • 17
  • 39
  • 1
    Like you said, fs.readFile doesn't do any sort of caching, and there is nothing wrong with the function you showed. If you call it twice with two different file paths, and those two files have different content, it will return the correct, different content for each file. Here's some code you can run to prove it https://pastebin.com/ZNXc14nX so the problem must be elsewhere. Check how the result of this function is actually used. – Aurast Jul 22 '21 at 21:33
  • I suspect the problem here is in how you are calling and using the result from the `getFile()` function. I would suggest you show the calling code where you think you see the problem. There is no nodejs caching here at all. Also, your error handling for this function is flawed because the caller cannot catch your errors in any way. You need to communicate back an error via the callback too. – jfriend00 Jul 23 '21 at 03:32

0 Answers0