title: deleting an image from file system node.js
body: <p>I have the following route for uploading profile images. Each user can have only one profile image, so as the new image is uploaded I want to delete the old image from the file system and mongo db. So far I have been attempting to delete the file from the file system using fs.unlink. Following is the code for my post route:</p>
  
  <pre><code>app.post('/upload', ensureAuthenticated, (req, res) =&gt; {
  
      uploadDir = path.join(__dirname, './public/uploads/');
      let oldFileName = '';
  
      Image.findOne({ member_id: req.user.member_id })
          .then(oldImage =&gt; {
              oldFileName = oldImage.imageName;
  
              console.log(uploadDir + oldFileName);
              fs.unlink(uploadDir + oldFileName, (err) =&gt; {
                  console.log(err);
              });
          }).catch(error =&gt; {
              console.log(error);
          });
  
  
      upload(req, res, (err) =&gt; {
          if (err) {
              res.render('index', {
                  msg: err
              });
          } else {
  
              const newImage = {
                  imageName: req.file.filename,
                  image_caption: req.body.image_caption,
                  member_id: req.user.member_id
              }
              new Image(newImage)
                  .save()
                  .then(image =&gt; {
  
                      req.flash('success_msg', 'Image added');
                      res.redirect('/');
                  })
                  .then(resize =&gt; {
                      let imgHeight = 150;
                      let imgWidth = 150;
                      sharp(req.file.path)
                          .blur(.3)
                          .toFile('public/thumbs/blurred_' + req.file.originalname, function (err) {
                              if (!err) {
                                  req.flash('success_msg', 'Thumbnail created');
                                  //res.redirect('/');
                              }
                          });
                  })
                  .catch(error =&gt; {
                      console.log(error);
                  });
  
  
          }
      });
  });
  </code></pre>
  
  <p>The problem I am facing is:</p>
  
  <ul>
  <li>How to handle the situation where the user is uploading for the first
  time and does not have any old images.
  when I try to delete I get the following error:</li>
  </ul>
  
  <p>TypeError: Cannot read property 'imageName' of null
      at Image.findOne.then.oldImage (C:\Users\Amin Baig\Desktop\Projects\photogallery2\app.js:130:36)</p>
  
  <p>Also, it tells me that it cannot find the file although I can see the path and file in the error.</p>
  
  <pre><code>{ Error: EBUSY: resource busy or locked, unlink 'C:\Users\Amin Baig\Desktop\Projects\photogallery2\public\uploads\I7SN7PAEl_1531803568428_dont_judge_me-wallpaper-1920x1080.jpg'
    errno: -4082,
    code: 'EBUSY',
    syscall: 'unlink',
    path: 'C:\\Users\\Amin Baig\\Desktop\\Projects\\photogallery2\\public\\uploads\\I7SN7PAEl_1531803568428_dont_judge_me-wallpaper-1920x1080.jpg' }
  </code></pre>