Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
How to Patch a user file containing files like image or txt file
router.patch ('/:id', upload.single ('profilePic'), async (req, res) => {
  try {
    // const prevUser = await User.findById (req.params.id).lean ().exec ();

    // const prevPath = prevUser.profile_pic;

    //delete prevfile using prevPath.
    const del = await User.findById ({_id: req.params.id}, function (
      err,
      data
    ) {
      console.log (data);
      if (err) throw err;
      fs.unlink (data.profile_pic);
    });
    console.log (del);
    
    const newUser = await User.findByIdAndUpdate ({
      _id: req.params.id,
      first_name: req.body.first_name,
      last_name: req.body.last_name,
      profile_pic: req.file.path,
    });

    return res.status (201).send ({newUser});
  } catch (e) {
    return res.status (500).send ({message: e.message, status: 'Failed'});
  }
});

  1. I want to update this file and add a different profile pic, i am not getting how to update it as is a form-data, while updating new profile i want to unlink previous one.
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!