POST route with validation error handling

PHOTO EMBED

Mon Oct 11 2021 07:12:17 GMT+0000 (Coordinated Universal Time)

Saved by @rook12 #javascript

app.post("/properties", async (req, res, next) => {
  try {
    const { body } = req;
    const property = new PropertyModel(body);
    await property.save();
    return res.status(201).send(formatProperty(property));
  } catch (error) {
    if (error.name === "ValidationError") {
      error.status = 400;
    }
    next(error);
  }
});
content_copyCOPY