TypeError: Cannot read property 'push' of undefined in Vuejs
axios.get('/getid').then(function(res){
this.todoList.push({
id: res.data[0].id,
todo_title: this.todo,
todo_status: 0
})
}).catch(function(res) {
console.log(res);
})
Data:
data() {
return {
todoList: []
}
}
What is the problem to push here??
Use arrow functions or bind this to preserve the scope of this
axios.get('/getid').then((res) => {
this.todoList.push({
id: res.data[0].id,
todo_title: this.todo,
todo_status: 0
})
}).catch((res) => {
console.log(res);
})
Can you post the res variable data, It might be empty.
@wingly,
Only printing the this.todo is not getting any value inside the push.
He already replied to you with a solution. You are inside function so this is unknown. Save this in a variable like
const self = this;
or use arrow syntax
(res) => {
// your code
}
Please or to participate in this conversation.