Nuxt.js: Dynamic Routes generates routes for non existent endpoints

Created on 21 Sep 2017  路  4Comments  路  Source: nuxt/nuxt.js

Hi.

I have such setup.

setup

First I thought everything fine

ok

But then I notice that Nuxt generates routes for any value and doesn't return Error

error

This endpoint moooooney doesn't exist of course. And If I go to this route I got an error like it should:

404

How to setup everything, so Nuxt understand, that to show only the data that exist?

This is my articles/_section/index.vue file

asyncData ({ req, params, error }) {
    return axios.get(`${process.env.baseUrl}/api/articles/${params.section}`)
      .then((res) => {
        return { articles: res.data }
      })
      .catch(() => {
        error({ statusCode: 404, message: 'Section not found' })
      })
  }

This is my articles/_section/_slug.vue file

asyncData ({ req, params, error }) {
    return axios.get(`${process.env.baseUrl}/api/articles/${params.section}/${params.slug}`)
      .then((res) => {
        return { article: res.data }
      })
      .catch(() => {
        error({ statusCode: 404, message: 'Pooooost not found' })
      })
  }

I have only 5 sections in my API data.

This question is available on Nuxt.js community (#c1520)
help-wanted

Most helpful comment

This is great. Fix was so easy. Thanks @vuchl

exports.getArticleBySlug = (req, res, next) => {
  Article.findOne({section: req.params.section, slug: req.params.slug})
    .then((article) => {
      if (!article) {
        return res.status(404).json({
          message: 'Article doesn\'t exist'
        })
      }
      return res.status(200).json(article)
    })
    .catch((err) => {
      return next(err)
    })
}

All 4 comments

since you get to see an article it means that the

axios.get(`${process.env.baseUrl}/api/articles/${params.section}/${params.slug}`)

returns an article. are you sure your backend is checking the article to be in the section?

@vuchl looks like I am not

exports.getArticleBySlug = (req, res, next) => {
  Article.findOne({slug: req.params.slug})
    .then((article) => {
      if (!article) {
        return res.status(404).json({
          message: 'Article doesn\'t exist'
        })
      }
      return res.status(200).json(article)
    })
    .catch((err) => {
      return next(err)
    })
}

I am only checking if article is exist, not section. Hmmm...

This is great. Fix was so easy. Thanks @vuchl

exports.getArticleBySlug = (req, res, next) => {
  Article.findOne({section: req.params.section, slug: req.params.slug})
    .then((article) => {
      if (!article) {
        return res.status(404).json({
          message: 'Article doesn\'t exist'
        })
      }
      return res.status(200).json(article)
    })
    .catch((err) => {
      return next(err)
    })
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments

danieloprado picture danieloprado  路  3Comments