javascript - Node.js sporadically failing to write file with: Error: ENOENT, open 'filename' -


I am working on a node.js application which accesses the file system. In my trial, I've noticed something very strange that Google is not able to answer for me.

My code works up to 95% of the time, however, on occasion, I get this error: "Error: ENOENT, open 'test.Q3rax'" where test.Q3rax generates a random File name is That file should be writable, and if I run my code again, in which the file name is hard, then it works fine.

Here is the code where this has failed:

  NPJSON.protipip.litefile = function {new data} {if (loaded!!) (Error!): Error: NPJSON The written file () was called before any data was loaded. "; Var stringToWrite = JSON.stringify (new data); Fs.writefile (this.fileName, stringToWrite, function (mistake) {if throw (mistake) throw;}); };  

Edit: For clarification, I am generating random file names like this:

  var filename = 'test.' + GetRandomText (5,6); Function getRandomText (numberOfCharactersLow, numberOfCharactersHigh) {var numbChar = getRandomInt (numberOfCharactersLow, numberOfCharactersHigh); Var possible = "ABCDEFGHIJLLMNOPQRTUVWXAABDFGILMNNAPCrustWwox 1234567890"; Var returnText = ''; (Var i = 0; i & lt; numbChar; i ++) {returnText + = possible.charAt (getRandomInt (0, 62)); } Return return text; } Function getRandomInt (minimum, maximum) {return Math.floor (Math.random () * (max - min + 1)) + minute; }  

It is possible that you are not filing files in the node with written asynchronous Written O_SYNC means that you are trying to access the file before flushing it into the disk by the kernel. Here's a little bit more read here (under header fs.writeFile () ):

Edit : To display this problem forcibly An example is:

var fs = is required ('FS'); Var fname = '/ tmp / fs.' + Process.pid + '.tmp'; Var buf = new buffer (1024); Buf.fill ('one'); Fs.writeFile (fname, buf, function (error) {console.log (err);}); Try Fs.readFileSync (fname); } Hold (e) {console.log (e.stack); } Process.on ('exit', function) (try {fs.unlinkSync (fname);} hold (e) {}}); // output: // error: ENOENT, such a file or directory '/tmp/fs.31281.tmp'

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -