You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having an issue that an error is returned to the callback if a file with the given name already exists.
My format string is is %f%i. What I'm trying to do is preserve the original file name as much as possible, but when a second file.txt is uploaded, name it something like file2.txt. This seems like a pretty standard use-case.
What happens is, main.js:52 calls fs.exists. Upon finding that the file exists, main.js:60 calls back with the "error", which is actually just true. Error handlers then log true and nothing more (that took awhile to debug!).
I think there are two issues here:
An error should be constructed, so there will be a message and a stack trace.
If I'm understanding correctly, it seems it should increment the counter (%i) and try again. Am I wrong?
Looking a bit deeper I think I see the issue. fs.exists calls back with a single argument (very unconventional for Node.js, and apparently the main reason for its deprecation.), telling whether the file exists. The onExists function (main.js:58) expects the first argument to be err. Hence, when the file exists, it calls back with true rather than trying another iteration.
This might be a good time to switch to fs.open, probably using the wx+ mode (fs.exists being deprecated). In that case it would be necessary to catch errors from fs.open that will be thrown if the file exits already.
I can't commit to anything, but I might be able to submit a pull request.
The text was updated successfully, but these errors were encountered:
I'm having an issue that an error is returned to the callback if a file with the given name already exists.
My format string is is
%f%i
. What I'm trying to do is preserve the original file name as much as possible, but when a secondfile.txt
is uploaded, name it something likefile2.txt
. This seems like a pretty standard use-case.What happens is, main.js:52 calls
fs.exists
. Upon finding that the file exists, main.js:60 calls back with the "error", which is actually justtrue
. Error handlers then logtrue
and nothing more (that took awhile to debug!).I think there are two issues here:
%i
) and try again. Am I wrong?Looking a bit deeper I think I see the issue. fs.exists calls back with a single argument (very unconventional for Node.js, and apparently the main reason for its deprecation.), telling whether the file exists. The
onExists
function (main.js:58) expects the first argument to beerr
. Hence, when the file exists, it calls back withtrue
rather than trying another iteration.This might be a good time to switch to
fs.open
, probably using thewx+
mode (fs.exists
being deprecated). In that case it would be necessary to catch errors fromfs.open
that will be thrown if the file exits already.I can't commit to anything, but I might be able to submit a pull request.
The text was updated successfully, but these errors were encountered: