I have the following update route:
router.put('/:id', upload.single('userImage'), (req, res) => {
Users.findById(req.params.id)
.then(user => {
user.shareEmail = req.body.shareEmail,
user.filmmakerQuestion = req.body.filmmakerQuestion,
user.genres = req.body.genres,
user.favoriteFilm = req.body.favoriteFilm,
user.imbd = req.body.portfolio,
user.portfolio = req.body.portfolio,
user.aboutYou = req.body.aboutYou,
user.userImage = req.file.path
user
.save()
.then(() => res.json("The User is UPDATED succesfully!"))
.catch(err => res.status(400).json(`Error: ${err}`));
})
.catch(err => res.status(500).json(`Error: ${err}`));
console.log(req.body);
console.log(req.file);
});
The issue that I am having is that if I send a request to only update:
{
"shareEmail": "yes,
"filmmakerQuestion": "no"
}
it also overrides those values not defined and re-sets those values. So if before favoriteFilm, genres, portfolio, etc. had values before, they will be overridden as undefined now. So while I want to keep all other values as they were before, they are now undefined. So the request I really send looks more like:
{
"shareEmail": "yes,
"filmmakerQuestion": "no",
"genres": undefined,
"favoriteFilm": undefined,
"imbd": undefined,
"aboutYou": undefined,
"userImage" : undefined
}
I only want to update the fields specified and leave the other values as is. How do I handle this in a single request?
Thank you!