0

I'm trying to select a value of an object in a nested array. Data being returned from the API looks like this

[
   {
      "id":1,
      "data":value,
      "posts":[
         {
            "data":value,
            "user":{
               "data":value
            }
         }
      ]
   },
   {
      "id":2,
      "data":value,
      "posts":[
         {
            "data":value,
            "user":{
               "data":value
            }
         }
      ]
   }
]

And my HTML looks like this (items represents what was received from API, fetched by an appropriate method)

<my-component v-for="item in items" :key="post.id">
 {{ item.posts[0].user.data }}
</my-component>

Page data

data: () => ({
        items: []
    }),
beforeRouteUpdate (to, from, next) {
    this.search(to.params.query)
    next()
},
methods: {
   search() {
      // search stuff then response recieved thanks to Axios
      this.items = response.data
    }
}

Component data

<template>
  <div class="card facebook-card">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'my-component'
}
</script>

Returns an error because saying the value is null. I tried to console log the data in the output - it also returned null, but item.posts[0] returns as expected. What's going on here? How do I access the sub array properties?

22
  • Does your component wait for the data to be retrieved before rendering? If not, what does the original data look like? Commented Jan 24, 2018 at 2:58
  • @Phil Thanks for the heads up! The component renders everything fine such as "post.data, but post.posts[0].data or anything from the nested posts doesn't work... Commented Jan 24, 2018 at 3:02
  • Your data still isn't valid JSON. You can't have two separate objects like that unless they were in an array Commented Jan 24, 2018 at 3:03
  • Also, the key binding should represent a unique / identifiable property on the iteration item. Commented Jan 24, 2018 at 3:06
  • 1
    @Phil don't know what the heck happened, but a computer and server restart solved the issue! How strange... Commented Jan 24, 2018 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.