[Vuejs]-"this.grades.push is not a function" When trying to add a Grade to an array

1👍

Web storage stores strings, so the problem is in mounted:

this.grades = localStorage.getItem("grades");

Now, this.grades is a string, which doesn’t have a push method.

Store your array as JSON:

localStorage.setItem("grades", JSON.stringify(this.grades));

and parse it on retrieval:

this.grades = JSON.parse(localStorage.getItem("grades")) || [];

The || [] part gives you a blank array if the item isn’t in storage (getItem returns null, which will get converted to "null" and then parsed by JSON.parse back to null, which is falsy).

Leave a comment