0

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

3
  • 3
    If grd is a key, then try adding quotes around that line localStorage.getItem('grd'); Commented Sep 6, 2018 at 15:15
  • 2
    The getItem function need an string. Try this: localStorage.getItem('grd') Commented Sep 6, 2018 at 15:16
  • @Dylan thanks, I added quotes on all 'name' and 'grd' and it works, If you can put a quick answer, that would be great Commented Sep 6, 2018 at 15:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.