Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Validation and Handling Errors

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,676 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);
    }
  })
);