fire frost
fire frost

Reputation: 437

how should I declare localStorage variable to avoid a Reference Error

I have discovered 'localStorage' with this post recently and I am using it to keep an user logged in even if he refresh the web page.

when I only had to save the name of the user, I had no errors, but then, I added a 'grd' variable to save and I got this error:

[Vue warn]: Error in created hook: "ReferenceError: grd is not defined"

This is how I fetch the data when the page loads :

var name = localStorage.getItem(name);
var grade = localStorage.getItem(grd);
if (name !== null){
  this.user = name;
}
if (grade !== null){
  this.userGrade = grade;
}

and this is how I save the data :

localStorage.setItem(name, '');
localStorage.setItem(grd, '');

What should I change/add to clear the Reference error?

I am using vue.js in this project

Upvotes: 0

Views: 51

Answers (1)

Dylan
Dylan

Reputation: 1714

You can resolve the reference error by adding quotes to the variables grd and name like so:

var name = localStorage.getItem('name');
var grade = localStorage.getItem('grd');

The referenceces are key values and must be passed as a string.

Upvotes: 1

Related Questions