Multiple calls when dynamic routes?

hackuun
96
hackuun
commented 2 years ago

Hi. What if I need to make multiple calls to API to generate routes?

routes () {
      return axios.get(`http://example.me/articles/crypto`)
        .then(res => res.data.map(article => '/crypto/' + article.slug))
      return axios.get(`http://example.me/articles/ico`)
        .then(res => res.data.map(article => '/ico/' + article.slug))
    }

This obviously doesn't work. What should I do in such case?

0
NicoPennec
1.6k
NicoPennec
commented 2 years ago

you may merge your requests with axios.all()

eg.

return axios.all([
    axios.get('https://jsonplaceholder.typicode.com/users'),
    axios.get('https://jsonplaceholder.typicode.com/ico')
])
.then(axios.spread((users, ico) => [...users.data, ...ico.data]))
0
hackuun
96
hackuun
commented 2 years ago

Thanks for response, but I am not sure I following how to use this ES6 features.

return axios.all([
        axios.get('http://example.me/articles/crypto'),
        axios.get('http://example.me/articles/ico')
      ])
        .then(axios.spread((crypto, ico) => [
          ...crypto.data.map(article => '/crypto/' + article.slug)),
          ...ico.data.map(article => '/ico/' + article.slug))
        ]))
0
hackuun
96
hackuun
commented 2 years ago

Oh, I had extra bracket It's working! Thanks a bunch @NicoPennec for help and this awesome module

0
hackuun
96
hackuun
commented 2 years ago

What about my other question, how to use this

{
        url: '/page/2',
        changefreq: 'daily',
        priority: 1,
        lastmodISO: '2017-06-30T13:30:00.000Z'
}

With Dynamic routes? Right now it's assigning only url.

0
Informations
Question โ€ข Unresolved
#c7 - Created 2 years ago