Hi.
I have such setup.

First I thought everything fine

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

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

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.
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.
Most helpful comment
This is great. Fix was so easy. Thanks @vuchl