This is my vue code :
new Vue({
el : '#root',
data : {
blog : []
},
created() {
this.$http.get('https://jsonplaceholder.typicode.com/posts')
.then(function(response) {
// console.log(response.data)
this.blog = response.data
})
.catch(function (error) {
this.error = 'Error! Could not reach the API. ' + error
})
}
});
My html code is :
<div id="root" class="container">
<ul v-for="post in blog">
<li> {{ post.id }} </li>
<li>{{ post.userId }} </li>
<li>{{ post.title }} </li>
</ul>
</div>
Now I can show every user's name just fine, but I want to modify something like if user id is 1 then the user's name will be changed into "Smith". I tried this code:
mounted() {
if (this.blog[0].userId == 1) {
this.blog[0].userId = 'Smith'
}
}
But it shows this error :
Uncaught TypeError: Cannot read property 'userId' of undefined
If I used in method with event it works just fine! How to do this ?
Also after console.log(this.blog[0].userId) I get : "1"

Uncaught TypeError: Cannot read property 'name' of undefinedBut if i tried it with some event handler in method then it works fine! Exactly I want to modify data after saving it to array!console.log(this.blog)?