Nuxt.js: How to deal with multiple dynamic routes nuxt generate

Created on 3 Jul 2017  路  4Comments  路  Source: nuxt/nuxt.js

Hey, in nuxt guide there is example how to deal with dynamic routes using promise and call to axios, now, how to deal with two of them?

generate: {
    routes: function (callback) {
      let posts = axios.get('https://api.com/posts', {params: {size: 10}}).then((res) => {
        return res.data.posts.map((post) => {
          return '/feed/' + post.id
        })
      })
      let users = axios.get('https://api.com/users', {params: {size: 10}}).then((res) => {
        return res.data.users.map((user) => {
          return '/user/' + user.id
        })
      })
      let route = Promise.all([posts, users]).then(values => {       
        return callback(null, route)
      })
    }
},

This is my example - trying to connect two promises but still have error.

This question is available on Nuxt.js community (#c884)

Most helpful comment

OK, this is how it should looks:

generate: {
  routes: function () {
    let posts = axios.get('https://api.com/posts', {params: {size: 10}}).then((res) => {
      return res.data.posts.map((post) => {
        return '/feed/' + post.id
      })
    })
    let users = axios.get('https://api.com/users', {params: {size: 10}}).then((res) => {
      return res.data.content.map((user) => {
        return '/user/' + user.id
      })
    })
    return Promise.all([posts, users]).then(values => {
      return values.join().split(',');
    })
  }
},

All 4 comments

OK, this is how it should looks:

generate: {
  routes: function () {
    let posts = axios.get('https://api.com/posts', {params: {size: 10}}).then((res) => {
      return res.data.posts.map((post) => {
        return '/feed/' + post.id
      })
    })
    let users = axios.get('https://api.com/users', {params: {size: 10}}).then((res) => {
      return res.data.content.map((user) => {
        return '/user/' + user.id
      })
    })
    return Promise.all([posts, users]).then(values => {
      return values.join().split(',');
    })
  }
},

Any idea how to implement this with payloads as well?

@mattwaler you should be able to do the same thing, just return an object instead of string:

generate: {
  routes: function () {
    let posts = axios.get('https://api.com/posts', {params: {size: 10}}).then((res) => {
      return res.data.posts.map((post) => {
        return {
          route: '/feed/' + post.id,
          payload: post
        }
      })
    })
    let users = axios.get('https://api.com/users', {params: {size: 10}}).then((res) => {
      return res.data.content.map((user) => {
        return {
           route: '/user/' + user.id,
           payload: user
         }
      })
    })
    return Promise.all([posts, users]).then(values => {
      return [...values[0], ...values[1]]
    })
  }
},

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VincentLoy picture VincentLoy  路  3Comments

gary149 picture gary149  路  3Comments

danieloprado picture danieloprado  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

vadimsg picture vadimsg  路  3Comments