[Vuejs]-Got error when calculate percent, but code works

0👍

Ok, I managed to clear error:

in data() added activeProjects: []

then in computed:

filteredProjects() {
        let result = this.projects.projects;
        this.activeProject = result.filter(item => {
            return item.finished === 0
        });
    }

},

finnaly

<h3>{{ this.activeProject.length }} - {{ parseInt(this.activeProject.length/projects.projects.length*100) }}

1👍

In your data() function, add a default value that is an empty array:

data() { 
  return {
    this.projects = {};
    this.projects.projects = [];
  }
},

This allows for code that executes methods from the Array.prototype not to error while your this.projects.projects is not yet populated.

0👍

This is means that when you use function filteredProjects in result you have not array, maybe undefined, insert console.log(result) before you use result.filter and check what is in the result.

Leave a comment