0

I'm trying to use my sessionStorage to retrieve a user ID so that users can access information attached to them. I get the sessionStorage as a token but for some reason when I try access a parameter it comes out as undefined.

 var userToken = TokenService.getAuthToken();
        // getAuthToken() {
        //    return window.sessionStorage.getItem(config.TOKEN_KEY)
        //  },

       console.log('userKey: ' + userToken)
        //userKey: {"userName":"turtlesoup","password":"password","userID":"Z100"}
       console.log(userToken.userID);
        //undefined

edit: The issue isn't getting the session information. As I put in the sample code. I get the sessionStorage token as a object. The issue is for some reason the parameter of the object isn't working.

  • `sessionStorage.getItem` returns a string, not an object. You need to parse it before you access its properties. – Ivar Feb 21 '21 at 00:56
  • Regarding your edit: isn't that what I covered in my previous comment? Your `userToken` is a string, not an object. You need to `JSON.parse(userToken)` it first. – Ivar Feb 21 '21 at 09:40

0 Answers0