How parse data from local storage in the typescript file?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
How parse data from local storage in the typescript file?
I have a user-stored array in the local storage, each with login, password, role and email. This is data about registered users. I need to write this array into a variable in the typescript.I created a variable existingUsers
public existingUsers: login: string, password: string, email: string, role: string ;
and transferred json from local storage to it
logInUser()
this.existingUsers = JSON.parse(localStorage.getItem('allUsers'));
console.log(this.existingUsers[0].login);
but after that all the properties existingUsers, such as login of password, are undefined. What am I doing wrong?
existingUsers
existingUsers
1 Answer
1
this.existingUsers
is an object not an array, you need to access without using index
this.existingUsers
index
logInUser()
this.existingUsers = JSON.parse(localStorage.getItem('allUsers'));
console.log(this.existingUsers.login);
The code has it typed as an array though?
– user184994
just now
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's probably an issue when you initialise
existingUsers
. Can you please add the code where you set theexistingUsers
?– user184994
25 secs ago