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'});
}
});
- 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.