i am new to vuejs3, i tried using json with vuejs3, i displayed the courses in view blog / index, and when clicking on a course in an index it redirect to details of that course clicks, in my case it gives me error: Uncaught (in promise) TypeError: Cannot read property 'title' of null.
blog/index.vue
<template lang="">
<div class="row">
<div class="col-md-6" v-for="post in posts">
<h1><router-link :to="{ name: 'post-show', params: { id: post.id, slug: post.slug }}">{{ post.title }}</router-link></h1>
<p class="lead">{{ post.content }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
posts: []
}
},
mounted() {
fetch('http://localhost:5000/posts')
.then(res => res.json())
.then(data => this.posts = data)
.catch(err => console.log(err))
},
}
</script>
blog/show.vue
<template lang="">
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
</template>
<script>
export default {
props: ['id', 'slug'],
data(){
return {
post: null
}
},
mounted() {
fetch(`http://localhost:5000/posts/${this.id}`)
.then(res => res.json())
.then(data => this.post = data)
.catch(err => console.log(err))
}
}
</script>
Router/index.js
{
path: '/blog/:id/:slug',
name: 'post-show',
component: Show,
props: true
},
data/db.json
{
"posts": [
{
"id": 1,
"title" : "learn angular",
"slug" : "learn-angular",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
},
{
"id": 2,
"title" : "learn react",
"slug" : "learn-react",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
},
{
"id": 3,
"title" : "learn laravel",
"slug" : "learn-laravel",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
},
{
"id": 4,
"title" : "learn symfony",
"slug" : "learn-symfony",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
},
{
"id": 5,
"title" : "learn jee",
"slug" : "learn-jee",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
},
{
"id": 6,
"title" : "learn php",
"slug" : "learn-oracle",
"content": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, possimus."
}
],
"users": []
}
Cannot read property 'title' of nullinsideshow.vuecomponent. The datapostobject is initialized as null, and only thefetch()function is populating it. Which means that thefetch()function doesn't return a value, or it fails for some reason.