Home > Mobile >  How Do I Get Blog Articles By Category in Node.js Mongoose?
How Do I Get Blog Articles By Category in Node.js Mongoose?

Time:11-22

I'm working on a blogging app in Node.js. I have no problem displaying all articles on the homepage and then routing each of the blog articles by slug. However, I have a problem when I want to route a page where all the articles for that given category are displayed.

With the code below, the slug router works fine, but the category router doesn't work. Every time I click on the link to go to the category page, I instead get sent to the homepage. When I move the router for the category page above the one for the slug page the category page begins to work, but then the slug page starts to experience problems. That being, whenever I click on the link for the slug page, I get sent to the right url, but the html content is the same content from the category page.

//blog article page
articleRouter.get('/:slug', async (req, res) => {
    const article = await Article.findOne({ slug: req.params.slug });
    if(article == null) res.redirect('/');
    res.render('blog/show', { article: article });
});

//get blog article category page
articleRouter.get('/:category_slug', async (req, res) => {
    const articles = await Article.find({ category_slug: req.params.category_slug }).sort(
        { createdAt: 'descending' });
    res.render('blog/category', { articles: articles });
});

//edit blog article page
articleRouter.get('/edit/:id', async (req, res) => {
    const article = await Article.findById(req.params.id)
    res.render('blog/edit', { article: article });
});

The only way that I've been able to fix it is it to change the get parameter in the slug router like so:

//blog article page
articleRouter.get('/:category_slug/:slug', async (req, res) => {
    const article = await Article.findOne({ category_slug: req.params.category_slug, slug: req.params.slug });
    if(article == null) res.redirect('/');
    res.render('blog/show', { article: article });
});

However, upon doing this I can no longer access the edit page.

CodePudding user response:

I will recommend you follow the name convention for a rest API, you must define your resource in this case how would I do it:

For articles resource:

  • GET /articles- Retrieves a list of articles
  • GET /articles/12 - Retrieves a specific article
  • POST /articles - Creates a new article
  • PUT /articles/12 - Updates article #12
  • PATCH /articles/12 - Partially updates article #12
  • DELETE /articles/12 - Deletes article #12

repeat the example above with the categories.

to get all the articles by category slug you will add category_slug as a query.

example:

  • GET /articles?category_slug=<here the slug> - Retrives a lis of articles by category_slug

so your code will be:

//get all articles from the database or if category is specified, get all articles from that category
articleRouter.get('articles', async (req, res) => {
  const query = Article.find();

  if (req.query.category_slug) {
    query.where({ category_slug: req.query.category_slug });
  }

  const article = await query;

  res.render('<route of list of articles>', { article: article });
});

//get article by slug
articleRouter.get('articles/:slug', async (req, res) => {
  const article = await Article.findOne({ slug: req.params.slug });
  if (article == null) res.redirect('/');
  res.render('blog/show', { article: article });
});

//edit blog article page
articleRouter.get('articles/edit/:id', async (req, res) => {
  const article = await Article.findById(req.params.id);
  res.render('blog/edit', { article: article });
});
  • Related