Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Validation and Handling Errors

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 Points

Is there anything wrong with my code?

I solved the error handling for the router.post(":id/edit" ...) a bit differently that Guil. From what I can tell everything works the same. But I am wondering if my solution could cause potential issues that Guil avoided in his way?

router.post(
  "/:id/edit",
  asyncHandler(async (req, res) => {
    const article = await Article.findByPk(req.params.id);
    if (article) {
      try {
        await article.update(req.body);
        res.redirect("/articles/" + article.id);
      } catch (error) {
        if (error.name === "SequelizeValidationError") {
          res.render("articles/edit", {
            article,
            title: article.title,
            errors: error.errors,
          });
        } else {
          throw error;
        }
      }
    } else {
      res.sendStatus(404);
    }
  })
);