Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrperfectionist's avatar

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??

0 likes
4 replies
wingly's avatar

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);
})
mshoaibdev's avatar

Can you post the res variable data, It might be empty.

powajoj's avatar

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.