Sr New
Sr New

Reputation: 77

Exception in node when using mkdirsync

I am having a code.

    try{
        var Storage = multer.diskStorage({
            destination: function (req, file, callback) {
                fs.mkdirSync('/home/data/' + file.originalname, { recursive: true }, (error) => {
                    if (error) {
        } else {
                        alert('Directory created');
                        console.log('Your directory is made!');
                    } ........

}catch(e){
    console.log(e);
}

Even I have a try catch, my program is giving a exception when the directory is already present. I am not getting why ..the node is crashing and not procedding. can you please let me know if I am missing anything

Error I am getting is

fs.js:885
  return binding.mkdir(pathModule._makeLong(path),
                 ^

Error: EEXIST: file already exists, mkdir '/home/data/dd_dd'
    at Object.fs.mkdirSync (fs.js:885:18)
    at DiskStorage.destination [as getDestination] (/home/server.js:16:7)
    at DiskStorage._handleFile (/home/nodejsapplication/node_modules/multer/storage/disk.js:31:8)
    at /home/node_modules/multer/lib/make-middleware.js:144:17
    at allowAll (/home/node_modules/multer/index.js:8:3)
    at wrappedFileFilter (/home/node_modules/multer/index.js:44:7)
    at Busboy.<anonymous> (/home/node_modules/multer/lib/make-middleware.js:114:7)
    at emitMany (events.js:147:13)
    at Busboy.emit (events.js:224:7)

Upvotes: 0

Views: 2313

Answers (2)

Sunil Lama
Sunil Lama

Reputation: 4539

The error is because the directory exists, you either have to delete that dd_dd folder that exists or do a directory check if exists for your code to work

try {
  var Storage = multer.diskStorage({
        destination: function(req, file, callback) {
          var uploadPath = '/home/data/' + file.originalname;
          fs.exists(uploadPath, function(exists) {
            if (exists) {
              next();
            } else {
              fs.mkdir(uploadPath, {
                recursive: true
              }, (err) {
                if (err) {
                  console.log('Error in folder creation');
                  next();
                }
                alert('Directory created');
                console.log('Your directory is made!');
              })
            }
          })
        }
        catch (e) {
          console.log(e);
        }

Upvotes: 0

blex
blex

Reputation: 25634

The error is thrown inside asynchronous code (your callback to multer.diskStorage). So it's not going to be caught by your wrapping try/catch (it can only catch synchronous errors).

You're also using the synchronous version of mkdir, which is mkdirSync. It does not take a callback, as it's synchronous.

Either use mkdir, which expects a callback with an error parameter:

var Storage = multer.diskStorage({
    destination: function (req, file, callback) {
        fs.mkdir('/home/data/' + file.originalname, { recursive: true }, (error) => {
          if (error) {} else {
            console.log('Your directory is made!');
          }
        });
     // ...

Or keep mkdirSync, and add the try/catch around it:

var Storage = multer.diskStorage({
    destination: function (req, file, callback) {
      try {
        fs.mkdirSync('/home/data/' + file.originalname, { recursive: true });
        console.log('Your directory is made!');
      } catch (e) {}
     // ...

Upvotes: 1

Related Questions