Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice about how to fetch many times #1202

Closed
fabienheureux opened this issue Feb 18, 2017 · 3 comments
Closed

Advice about how to fetch many times #1202

fabienheureux opened this issue Feb 18, 2017 · 3 comments

Comments

@fabienheureux
Copy link
Contributor

Hi,

Sorry if there are already several open/closed issues on this exact topic (#192 this one for example), but I want to achieve something, and I have some difficulties to figure out either if it is a react or next.js issue.

Here is the thing:

  1. I call an API to fetch some data, in pages/index.js, inside getInitialProps()
  2. From the api result, I need to call a second time, in order to re-fetch some data (based on the first call result), and inject these into a component.

Ideally, I would have fetch one time, and pass the resulting data to a child component, in which I would have fetched a second time inside componentWillMount().

As it should be isomorphic in nextjs, it won't work, and I really don't know where should I call the api the second time.

Without nextjs version: (the digests props is passed as a prop to a child component)

static async getInitialProps () {
  const page = await fetch(`${baseURL}/pages/640/`)
  const homepage = await page.json()
  
  return {
    digests: homepage.homepage_digests,
  }
}

With next.js, what I tried to do, without success:


static async getInitialProps () {
    const page = await fetch(`${baseURL}/pages/640/`)
    const homepage = await page.json()
    const digestsList = await homepage.homepage_digests.map(async (digest, index) => {
      const res = await fetch(`${baseURL}/pages/${digest.digestId}/`)
      const data = await res.json()
      return data
    });

    return {
      digests: digestsList
    }
  }

Sorry if this question is dumb or already answered, as I said, I found many related issues, but really can't figure how it could be used in my specific case....

Thanks a lot in advance 👍

@jonaswindey
Copy link
Contributor

I think that array.map() and array.forEach() are executed async when you combine them with async/await. That's why your function probably returns before execution is complete.

Could you try to replace the .map() with

for (let digest of homepage.homepage_digests) {
  // fill your data object here
}

That should do it.

Another idea is to use Promise.all()

See here: http://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop

@timneutkens
Copy link
Member

@jonaswindey you're totally right 🙌

@fabienheureux
Copy link
Contributor Author

fabienheureux commented Feb 19, 2017

Well, that did the trick.
Thanks so much @jonaswindey, it was definitely not a nextjs issue, neither a react one but a js one...you saved me some big headaches 😉

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants