node-ftp icon indicating copy to clipboard operation
node-ftp copied to clipboard

mkdir throwing error but still creates the directory

Open FlandersBurger opened this issue 7 years ago • 2 comments

I don't know why it is giving me an error since it is creating the directory. I can put an empty error callback function in mkdir but I'd rather know how to prevent the error

Below is the code

var FTP = require('ftp');
router.post('/:item/files/add', function (req, res, next) {
  var ftp = new FTP();
  ftp.on('ready', function() {
    ftp.mkdir(req.params.item);
    ftp.end();
    return res.sendStatus(201);
  });
  ftp.connect(config.ftp);
});

and here is the error.

XXXXXXXX\node_modules\ftp\lib\connection.js:113
        self._curReq.cb(makeError(code, text), undefined, code);
                     ^

TypeError: self._curReq.cb is not a function
    at Parser.<anonymous> (XXXXXXXX\node_modules\ftp\lib\connection.js:113:22)
    at emitTwo (events.js:100:13)
    at Parser.emit (events.js:185:7)
    at Parser._write (XXXXXXXX\node_modules\ftp\lib\parser.js:59:10)
    at doWrite (_stream_writable.js:292:12)
    at writeOrBuffer (_stream_writable.js:278:5)
    at Parser.Writable.write (_stream_writable.js:207:11)
    at Socket.ondata (XXXXXXXX\node_modules\ftp\lib\connection.js:273:20)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)

FlandersBurger avatar Mar 07 '17 17:03 FlandersBurger

I was getting errors like this, and it was when I called those methods the same way you are. Try doing something like this:

ftp.mkdir(req.params.item, true, function (err) {
    if (err) {
        throw (err);
    }
});

Feel free to leave true out if you don't need it to be recursive. Hopefully that helps you out!

theaaronmartin avatar Mar 07 '17 20:03 theaaronmartin

I have another problem with this method. I confused with mkDir function. On my localhost, during tests, putting true as a parameter means that it would create a directory or if it exists the function won't return an error. Everything was OK, but on production server, it crashes and sometimes returns Error: File exists when directory exists How can I solve this problem? My FTP connection creates a couple of folders asynchronously, maybe doing it in sync way will solve this problem? Cannot reproduce it locally, but It seems like parameter true should work fine. Also I create folder with subfolder, maybe I shouldn't do it? c.mkdir(token + '/' + jsonCampaign.base.token , true, (err, files) => {

EugeneHerasymchuk avatar May 19 '17 12:05 EugeneHerasymchuk