this question about backend development node.js how do i update a database data by using object id
please help me to learn those things custom validator
app.patch("/users/:id", async (req, res) => {
try {
const user = await User.findByIdAndUpdate(req.params.id, req.body, {
runValidators: true,
new: true,
});
if (!user) {
return res.status(404).send();
}
res.send(user);
} catch (e) {
res.status(400).send()
}
});
this my code please modify to add validators like allowedUpdates …
2
Answers
You can use mongoose ObjectId validator:
Update your code to look like this:
You can try like this :