4

I have these two routes /services and /services/parents and each page is stored in a vue file like this:

// File location : "/pages/services/index.vue"

<tr v-for="service in services" :key="service.id">
  <td class="check-col"><input type="checkbox"></td>
  <td class="number-col">{{ service.id }}</td>
  <td>{{ service.name }}</td>
  <td>{{ service.slug }}</td>
  <td>
    <template v-if="service.children.length">
      <template v-for="(child, index) in service.children">
        <nuxt-link :to="'/admin/services/' + child.id" :key="child.id">
          {{ child.name }}
        </nuxt-link>

        <span :key="child.id" v-if="(index+1) < service.children.length">, </span>
      </template>
    </template>

    <span v-else>--</span>
  </td>
</tr>

and in my /services/parents page, i have:

// File location : "/pages/services/parents.vue"

<tr v-for="service in services" :key="service.id">
  <td>{{ service.id }}</td>
  <td>{{ service.name }}</td>
</tr>

<script>
export default{
  computed: {
    services() {
      return this.$store.getters['content-lists/services/list']
    }
  },
  async fetch({ store, query }) {
    let page = Number(query.page) || 1
    let perPage = Number(store.getters['content-lists/services/perPage'])
    let offset = (page - 1) * perPage

    const max = await strapi.request('get', '/services/count')
    store.commit('content-lists/services/setMaxPages', max)

    store.commit('content-lists/services/empty')
    const response = await strapi.request('post', '/graphql', {
      data: {
        query: `query {
          services(limit: ${perPage}, start: ${offset}) {
            id
            name
            slug
            children {
              id
              name
            }
          }
        }`
      }
    })

    response.data.services.forEach((service) => {
      store.commit('content-lists/services/add', {
        id: service.id || service._id,
        ...service
      })
    })
  }
}
</script>

When i navigate to parents page it gives me this error:

service.children is undefined

An error occurred while rendering the page. Check developer tools console for details.

It only happens when navigating with <nuxt-link> if you type the url in browser address it works just fine. I also checked console but there is no traces it just says : Type error: service.children is not defined

I appreciate any help in advance

Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
Amin
  • 1,637
  • 1
  • 22
  • 40
  • How are you variables defined in you data? I want to see where service comes from, and whats inside – Ferrybig Feb 02 '19 at 17:57
  • Hi @Ferrybig , i updated the question – Amin Feb 03 '19 at 06:02
  • How the data variable defined in `/pages/services/index.vue` file ?. Please provide response data for `services ` variable ? – Vasu Kuncham Feb 03 '19 at 08:33
  • could you try to modify this line? ` – gleam Feb 04 '19 at 07:27
  • 2
    potentially related (asyncData vs fetch) - could be 1) that navigating to `nuxt-link` is not fetching data for the component (check vuex store) 2) could also be that the service.children is not defined as stated above - therefore `service?.children` or `service.hasOwnProperty("children")` may help https://stackoverflow.com/questions/49251437/difference-between-asyncdata-vs-fetch – Denis Tsoi Feb 08 '19 at 15:13

0 Answers0