6

I get the error

TypeError: Cannot read property ‘name’ of undefined”

if I go to deep in the object.

Timeline.vue

<template>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="card card-default">
                <div class="card-header">Timeline</div>

                <div class="card-body">
                    <post v-for="post in posts" :key="post.id"></post>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import Post from './Post.vue'

export default {
    data () {
        return {
            posts: []
        }
    },
    components: {
        Post
    },
    mounted() {
        this.$http.get('/posts').then((response) => {
            console.log(response.body)
            this.posts =response.body
        })
    }
}
</script>

post.vue

<template>
<div class="media">

    <div class="media-left">

    </div>
    <div class="media-body">
        <strong>test</strong>
        <strong>{{ post.user.name }} </strong> 
    </div>
</div>
</template>

<script>
export default {
   props: [
        'post'
    ]
}
</script>

Then why I get the error? I suppose some problem with {{ post.user.name }}.

4
  • You should post the code executed from the /posts route. But if I had to guess, you're not loading the post user relation. So you should probably use eager loading Posts::with('user')->get() in your controller. When you're writing $post->user->name in PHP, Laravel makes sure the user is loaded on the fly, but if you return a JSON response in your controller without the loaded user, the client JS code can't load that relation data. Commented Apr 20, 2018 at 14:21
  • Adding either the post array or one post as an example would help out Commented Apr 20, 2018 at 15:10
  • Yes . I check localhost:8888/posts . It shows [{"id":1,"user_id":1,"body":"Necessitatibus cumque pariatur in excepturi.","created_at":"2018-04-20 07:48:56","updated_at":"2018-04-20 07:48:56","user":{"id":1,"name":"eunice","email":"[email protected]","created_at":"2018-04-05 08:20:13","updated_at":"2018-04-05 08:20:13"}}, Commented Apr 23, 2018 at 1:29
  • My post controller is below public function index(Request $request, Post $post) { return $post->with(['user'])->latestFirst()->get(); } Commented Apr 23, 2018 at 1:43

1 Answer 1

2

You need to pass the post prop in the template like so. otherwise it's undefined in the child component (post).

<div class="card-body">
    <post 
        v-for="post in posts" 
        :key="post.id"
        :post="post">
    </post>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

otherwise it's undefined in the child component this is not correct, the prop='post' in the child component='post' will be one string if pass post like <post post="post">; that is why the error threw at user.name not post.user

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.