I am working on a node.js application that accesses the file system. In my testing, I have noticed something very strange that google has not been able to answer for me.
My code works fine 95% of the time, however, on occasion, I get this error: "Error: ENOENT, open 'test.Q3rax'" where test.Q3rax is a randomly generate filename. That file should be writeable, and if I run my code again, with the filename hardcoded in, it works just fine.
Here is the code where it is failing:
npJSON.prototype._writeFile = function(newData) {
if (!this.loaded)
throw "ERROR: npJSON._writeFile() was called before any data was loaded.";
var stringToWrite = JSON.stringify(newData);
fs.writeFile(this.fileName,stringToWrite, function(err) {
if (err)
throw err;
});
};
Edit: For clarification, this is how I am generating the random filenames:
var filename = 'test.' + getRandomText(5,6);
function getRandomText(numberOfCharactersLow, numberOfCharactersHigh) {
var numbChar = getRandomInt(numberOfCharactersLow, numberOfCharactersHigh);
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
var returnText = '';
for (var i = 0; i < numbChar; i++) {
returnText += possible.charAt(getRandomInt(0, 62));
}
return returnText;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}