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

Databases Updating Entries in a Table

I don't really understand these two routes.

I think I know how the first one works. When you go to the edit route it finds the article that was chosen through the findByPk(req.params.id). Then I kind of get lost here.... does this function only find the article that you've chosen to edit then the PUT one actually performs the edit?

router.get("/:id/edit", function(req, res, next){
  Article.findByPk(req.params.id).then(article => {
    res.render("articles/edit", {article: article, title: "Edit Article"});
  });
});

Does this only actually edit the article

router.put("/:id", function(req, res, next){
  Article.findByPk(req.params.id).then(article => {
    return article.update(req.body)
  }).then(article => {
    res.redirect("/articles/" + article.id);
  });
});

1 Answer

Darryl Mah
Darryl Mah
5,492 Points

Yes!

The .findByPk method will only find the article.

The .update method is what will update the data.

So the first block of code finds the article and allows you to edit it. The second block of code finds the same article and the .update method will update it.

https://sequelize.org/master/class/lib/model.js~Model.html#static-method-update

Awesome. TY! That clears it up for me.