Remove aquifers with File System

Asked

Viewed 1,170 times

1

I am trying to remove two aquivos from my server with the following code:

const fs      = require('fs');
const express = require('express');
const app     = express();

app.delete('/delete', function(req, res){
   fs.unlink('path/doc_1', function(){
       fs.unlink('path/doc_2', function(){
           res.end();
       })
   })
})

But the following error is thrown:

UnhandledPromiseRejectionWarning: Error: EBUSY: resource busy or locked, unlink

Someone’s been through it?

  • The file is not open anywhere?

  • It is not, I saved it to the server with multer, and use this path to remove it

  • 1

    Change your file to this here and report the full stacktrace pf: https://pastebin.com/knbxWyUN

  • 1

    In the example I gave you now you have missed the word async before the (req, res)

  • Man, it worked perfectly. Thank you!

1 answer

2


Your mistake says the file is in use. The suggestion I give you is to make the following change so that, if any problem happens, it becomes clearer to identify it. With the change the files will be deleted in parallel also:

const fs      = require('fs');
const express = require('express');
const app     = express();

const { promisify } = require('util');
const unlink = promisify(fs.unlink);

app.delete('/delete', async (req, res) => {
  try {
    await Promise.all([unlink('path/doc_1'), unlink('path/doc_2')]);
    res.end();
  } catch(e) {
    console.error(e);
    res.status(500).send('Ocorreu um erro interno.');
  }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.