Handling results from "deep:true"
See original GitHub issue(Warning: I’ve already asked this question in the Reddit Nuxt.js subreddit and I still need help. If this makes my question irrelevant, I understand.)
TL,DR version: Nuxt.js beginner needs guidance on how to fetch from deeper levels with the Content Module, after hours of futile searching for instructions/examples/snippets about that.
If my content
directory is structured like this example…
/content
|_ posts
|_ 2020
|_ 02
|_ post.md
|_ post2.md
(edit after @benjamincanac’s reply reminded me I forgot to mention this)
…and I have this pages
structure:
/pages
|_ posts
|_ _slug.vue
(end of edit)
…and I use the following in an attempt to fetch from the deepest level…
<template>
<article>
<h1>Title = {{ article.title }}</h1>
<nuxt-content :document="article" />
</article>
</template>
<script>
export default {
async asyncData ({ $content, params, error }) {
const slug = params.slug || 'index'
const article = await $content('posts', { deep: true })
.where({ slug: slug})
.fetch()
if (!article) {
return error({ statusCode: 404, message: 'Article not found' })
}
return {
article
}
}
}
</script>
…how should I then handle the result so that the page appears?
If I do the above and go to http://localhost:3000/_content/posts/2020/02/post2
(note that it’s _content
rather than content
), I see good JSON from the result:
{"title":"post2","date":"2020-08-28T00:00:00.000Z","toc":[],"body":{"type":"root","children":[{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"This is another one."}]}]},"dir":"/posts/2020/02","path":"/posts/2020/02/post2","extension":".md","slug":"post2","createdAt":"2020-08-29T02:16:10.876Z","updatedAt":"2020-08-29T02:16:33.152Z"}
…proving it can “see” the post and, therefore, respects the deep:true
, but I get only a 404 if I go to http://localhost:3000/content/posts/2020/02/post2
. All the examples I’ve found in and out of the documentation seem to be for only one level below (in this case) /contents/posts/
; I can find none for fetching from a deeper level. It works fine if I put a Markdown file in /content/posts
, however, so clearly I’m not handling the deep:true
result properly.
Help, please?
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (3 by maintainers)
Top GitHub Comments
@brycewray - the functionality is there. I literally just launched my company website and the whole blog system is built on nuxt/content.
I’ll be updating the first blog article to describe how I’ve used Vuetify, NuxtJS and nuxt/content, but have a look https://www.olivegroveit.com.au
This is the markdown that created the first blog entry, plus the preview cards on the front page, the horizontal blog preview card, and the categories and archive filters:
@brycewray Have you made a
_.vue
file? It can’t work with_slug.vue
. Everything is explained here https://content.nuxtjs.org/snippets#dynamic-routing.