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 trialTom Madsen
Front End Web Development Techdegree Graduate 19,030 PointsFiltering the articles by author problem
Whilst attempting the challenge of filtering the articles by author, I've included an extra route path, but it seems that this path interferes with the "router.get("/:id" path. If I put my code above this line, it doesn't work, but the "/:id" path does. If I put my code below the "/:id" code, My code works, but the "/:id" route displays an empty page.
Here is my code:
router.get("/:author", asyncHandler(async (req, res) => {
const articles = await Article.findAll({
where: {
author: req.params.author
}
});
if (articles) {
res.render("articles/index", {
articles,
title: req.params.author
});
} else {
res.sendStatus(404);
}
}));
I've turned the "by_line.pug" author line into an anchor tag - to access this route.
OK - so I've found the solution to my problem - and here it is if anyone else comes up with the same issue:
The issue was that I had 2 "router.get('/:___" functions, one being for ':id' - to select the specific article, the other being for ':author' - to select articles by a certain author - both of which were on the same directory path. This meant that as soon as that root path was used, the renderer couldn't distinguish between either, and would run which ever code cascaded first in the file - causing issues with the second function.
To overcome this, I changed the route.get path for ':author' to '/article/:author'and changed the corresponding anchor link to find that path. No more problems...