javascript - NodeJS - stat doesn't always run -
here's code:
console.log("path 1: " + fullname); fs.stat(fullname, function(err, stats) { console.log("path 2: " + fullname); }, function(err) { // don't know if part console.log("an error occurred: " + err); // saw in different answer });
the code doesn't run files. in, logs show path 1 file not path 2 file, , none of "an error occurred". thinking maybe files have invalid character, because have equal signs in them. this:
...file.device_type=mobile.jsx
even if that's case, why no error or anything? , if so, how can stat these files?
you aren't logging or checking error. fs.stat()
accepts 2 parameters only, filename , callback function. passing 3 parameters, filename , 2 separate callbacks. second callback wrong. then, in first callback, need check err
variable see if error occurred.
this correct usage:
fs.stat(fullname, function(err, stats) { if (err) { console.log("error in fs.stat(): ", err); } else { console.log("got stats: ", stats); } });
if you're using proper form , still don't see either message in console, i'd suggest putting exception handler around see if else going on:
try { console.log("about call fs.stat()"); fs.stat(fullname, function(err, stats) { if (err) { console.log("error in fs.stat(): ", err); } else { console.log("got stats: ", stats); } }); } catch(e) { console.log("fs.stat() exception: ", e); }
in looking @ source code fs.stat()
, there several ways throw synchronous exception, particularly if detects invalid arguments passed it. usual, node.js documentation not describe behavior, can see in code. code fs.stat()
:
fs.stat = function(path, callback) { callback = makestatscallback(callback); if (handleerror((path = getpathfromurl(path)), callback)) return; if (!nullcheck(path, callback)) return; var req = new fsreqwrap(); req.oncomplete = callback; binding.stat(pathmodule._makelong(path), req); };
both makestatscallback()
, handleerror()
can throw exception (when @ implementations in same file).
i not know got notion of passing 2 callbacks fs.stat()
. documented here, not accept 2 callback functions. looks remotely promisified version of fs
library every async operation returns promise , can pass 2 callbacks fs.statpromise.then(fn1, fn2)
, have no idea if that's saw or not.
Comments
Post a Comment