I am facing an issue while trying to save information from an HTTP request into a structure and then store it in local storage. When I attempt to retrieve the stored item and print a specific value from the structure, it does not work!
Here is my code:
//Processing the response received from the HTTP request.
.then(function(response) {
//Storing necessary User Information in a struct.
let CurrentUser = {
username: response.data.result.User.username,
name: response.data.result.User.name,
token: response.data.result.token,
id: response.data.result.User.id,
}
//Saving user information in local storage.
localStorage.setItem("CurrentUser", JSON.stringify(CurrentUser));
var Curr_user = localStorage.getItem("CurrentUser");
console.log(Curr_user);
console.log("the name of the current user: " + Curr_user['name']);
})
The output I am receiving is:
{"username":"Myusername","name":"Myname","token":"XXXXXX","id":"YYYYYY"}
the name of the current user: undefined
I need to access the username or id stored in local storage for use in other pages.