router.post("/user", async (req, res) => {
try {
var user = new User(req.body);
await user.save();
res.status(200).send(user);
} catch (error) {
if (error.name === "ValidationError") {
let errors = {};
Object.keys(error.errors).forEach((key) => {
errors[key] = error.errors[key].message;
});
return res.status(400).send(errors);
}
res.status(500).send("Something went wrong");
}
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter