Problem getting user data in the application’s localStorage

Asked

Viewed 323 times

0

Guys I would like to implement a menu "my account" in my project, this menu should show the user data as name, email etc, I am using jwt and the logged in user data can be obtained in Localstorage in this format:{"id":"598a8bc8b9dd44250819a7c4","name":"bruno","username":"kinge880","email":"[email protected]"} I would just like to know how to access this array and get the name inside it I’m using nodejs, angular 4 and mongodb

  • localStorage.getItem("name")?

  • In localStorage I have only my user, then using getItem("user") I get that array up there, what I want to know is how to access the array and get a value inside it, in case, name

1 answer

0


The function localStorage#getItem returns a String, therefore it is necessary to make the conversion to object using JSON#parse:

var user = JSON.parse(localStorage.getItem('user'));
var name = user.name;

console.log(name);
  • That’s exactly what it was, thank you very much ;)

  • @Brunomaia Don’t forget to make the answer correct so that someone with similar doubt can take advantage of the solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.