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.

$content.fetch() inside a component

See original GitHub issue

Hello,

I’m quite lost and I need some help to understand why my code doesn’t work. I’m dev a very simple website with markdown files inside the contentfolder. In the root index.vue, I’ve been able to get the list of the markdown files, and building a menu in the navbar.

Wrapping up this in Vue style, I then created a component inside the components folder, and moved the <script> with asyncData getting the files via $content('', { deep: true}).fetch() It didn’t work. I remembered asyncData cannot be used inside a component, so I used :

(I’m in a component inside the components folder, and this component is displayed in a layout)

export default {
  data() {
    return { articles: {} };
  },
  async fetch(context) {
    this.articles = await context.$content("", { deep: true }).fetch();
  }
};

I just can’t figure out why it doesn’t work : the articles is still empty. I tried with and without context, I tried with this.$content… I’m clueless now.

My point is to make a static generated website.

Thanks a lot for your help, I just spent like 5 hours on this thing /o\

edit: actually it looks like fetch doesn’t exist. When I try to display fetchState, or when I put Property or method "$fetch" is not defined on the instance but referenced during render. based on the fetch examples I got some errors telling me the objects doesn’t exist.

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
mathieunicolascommented, Jun 4, 2020

Damn. One whole night spent on such a silly mistake. The package.json installed the good 2.12.x package, BUT I made a global install of nuxt few months ago, when I discovered the framework. This global package was still in 2.10.x so it couldn’t handle the “new fetch”.

npm install -g nuxt solved all my problems.

4reactions
mathieunicolascommented, Jun 4, 2020

Thanks for your answer, it’s one of the things I’ve tried during these hours…

export default {
  async fetch() {
    this.articles = await this.$content("", { deep: true }).fetch();
  },
  data() {
    return { articles: null };
  }
};

Doesn’t return any error, but the articles var is still null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt Content async fetch in component - vue.js - Stack Overflow
I'm new to the @nuxt/content module and it's working well except within components. Here I ...
Read more >
How to Do a Fetch Inside React Components - Medium
The fetch function returns a promise. We can chain functions executing on the success flow using .then() and functions executing on the error ......
Read more >
How to fetch data in React with performance in mind
It's relatively straightforward with a simple component: you just measure how long it takes to render it, and voila! The smaller the number,...
Read more >
Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >
A comprehensive guide to data fetching in React
The Fetch API provides a fetch() method defined on the window object, as well as a JavaScript interface for accessing and manipulating HTTP ......
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