3

Today I have to debug my todo App, but can't see the todo array elements in console.log

Code:

<template>
<!-- TEMPLATE FOR THE WHOLE APP -->
  <div class="container" @click="deleteTodo">

    <Todolist 
        :todos="todos" 
        :check="check" 
        :updateTodo="updateTodo" 
        :deleteTodo="deleteTodo" 
    />

  </div>
</template>

<script>
  import Todolist from './components/Todolist';

  export default {
    name: 'App',
    components: {
      Todolist,
    },
    data () {
      return {
        todos: [
          {
            id: 1,
            text: 'Making a cup of coffee',
            checked: true
          }, 
          {
            id: 2,
            text: 'Making an VueJS todo app',
            checked: false
          }, 
          ....
        ]
      }
    },
    methods: {
      deleteTodo: function(id) => {
        return console.log(this.todos[id]);
      }
    },
    
  }

I tried to do it in Parent and child components, but both didn't work, even if I try this.todos.

Also got an undefined message:

enter image description here

Can someone help me out?

Thanks in advance

3
  • I don't see any issue with the code, still I would say its not enough to debug this issue. We need to see the full code. Commented Feb 15, 2021 at 9:29
  • @BeshambherChaukhwan the error occured inside the methode code block, which I used an arrow function at deleteTodo: () => {, I changed it into function() {, and that worked Commented Feb 15, 2021 at 9:32
  • 1
    That's great :) Commented Feb 15, 2021 at 9:34

1 Answer 1

2

Make deleteTodo as a normal function, rather than arrow function.

Don’t use arrow functions on an options property or callback, such as created: () => console.log(this.a) or vm.$watch('a', newValue => this.myMethod()). Since an arrow function doesn’t have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.

Refer to here.

      deleteTodo: function() {
        console.log(this.todos);
      }
Sign up to request clarification or add additional context in comments.

13 Comments

Why is that. Does Vue.js not allow arrow functions inside an methode code block?
please check the below part of the link i provided.
It also contain an undefined after it is rendered. Do you know why?
@user12380607 May be this question answer your question
Since an arrow function doesn’t have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.