Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Vue warn]: Error in render: “TypeError: Cannot read property ‘name’ of undefined”

Tags:

vue.js

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 }}.

like image 664
Erison Li Avatar asked Apr 20 '18 14:04

Erison Li


1 Answers

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>
like image 174
Tarik Chakur Avatar answered Nov 10 '22 07:11

Tarik Chakur