question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to fetch from all child folders?

See original GitHub issue

I have this directory structure:

- content/
  - articles/
    - 2020/
    - 2019/
    - 2018/
  - projects/

I want to be able to fetch every .md file from the content/articles/ directory.

I have tried the following:

this.articles = await this.$content('articles/**/*').fetch();
this.articles = await this.$content('articles/*').fetch();
this.articles = await this.$content('articles/').fetch();
this.articles = await this.$content('articles').fetch();

Any ideas how I can achieve this?

Extra info: My first step was to place all articles inside the articles directory and fetch them from there, and it worked perfectly. I’m now looking to further split my articles into year directories.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jackdomleo7commented, Jun 13, 2020

I found a solution that was nearly what I wanted but I’m happy with it.

- content/
  - blog/
    - 2020/
    - 2021/
  -projects/
-pages/
  - blog/
    - _slug.vue
  - projects/

nuxt.config.js

generate: {
  async routes () {
      const { $content } = require('@nuxt/content');
      const files = await $content('', { deep: true }).only(['slug', 'dir']).fetch();

      return files.map(file => '/' + (file.dir.includes('blog') ? 'blog' : file.dir.includes('projects') ? 'projects' : '') + '/' + (file.slug === '/index' ? '/' : file.slug));
  }
}

@/pages/blog/_slug.vue

async asyncData ({ $content, params }) {
    const slug = params.slug || 'index';
    let page = await $content('blog', { deep: true })
      .where({ slug: slug })
      .only(['title', 'date', 'slug', 'description', 'readingTime', 'body'])
      .fetch();
    page = page[0]; // This was catching me out, for some reason it was returning an array with one object inside it, so I hard coded to always return the first index.

  return {
    page
  };
}

This solution allows me to keep the markdown files inside separate directories for their year, but the year is no longer in the URL.

E.g. @/content/blog/2020/example-1.md would appear at URL http://domain.com/blog/example-1 and I’m absolutely ok with that. Seems like the most logical solution. Thanks for your help @TheLearneer!

1reaction
TheLearneercommented, Jun 13, 2020

@JDomleo

const article = await this.$content('blog', { deep: true })
  // If your file names are unique across all the directories then you can simply check for slug..
  .where({ slug: 'filename' })
  // or you can even check the path if your file name is duplicated for various years.. below is just an example
  .where({ path:  '/year/article' })
  .fetch();

Also even if you do not are not planning to create a dedicated page for year itself, seems like you are still going for year/article format, so you will have to be careful there…


@jbnv Make sure your content module is updated to at least 1.3.0 because that is when the deep fetch was introduced… Also seems like you are trying to fetch the root content directory. I stronly suggest you to put your files in some sub directory because even if doesn’t make any sense now, you will realize later than doing so will be useful in many ways… That is because if you plan to add some other type of content in future then your code will throw error for sure and also fetching content from specific folder would be more safer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Run git pull over all subdirectories [duplicate] - Stack Overflow
Run the following from the parent directory, plugins in this case: find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} ...
Read more >
How do you git fetch all repos in a folder? - Ask Different
One can combine find . -type d -iname ".git" -exec echo {} \; and replace echo command with a script with the following...
Read more >
How to Retrieve Files from All Subfolders in Windows File ...
Get more tips for the awesome analyst at georgejmount.com.In this video we will look at how to retrieve files across subfolders with a...
Read more >
PowerTip: List all subfolders under a target path with PowerShell
Here's an example that targets drive c: and all hidden folders. It displays only directories and their full paths. Get-ChildItem -Path C ...
Read more >
Shell script to run `git pull` inside all subdirectories which are ...
Shell script to run `git pull` inside all subdirectories which are git repositories. I keep a number of projects in a folder &...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found