Sparsh Verma
Sparsh Verma

Reputation: 67

how to get value from session storage

So i have create a method which will be called when user click on login button. On this method i also store data on session storage which store the name and role of a user. So when i try to get data from session Storage i am getting [object Object] in console. How to get data from it.

onSubmit() {

    this.submitted = true;

    this.loaderService.display(true);

    try {

      if (!this.loginForm.invalid) {

        this.login

          .Login(this.f.UserName.value, this.f.Password.value)

          .pipe(first())

          .subscribe(

            (data: Login) => {

              if (data.token) {

                this.loaderService.display(false);

                let user = this.jwtHelperService.getDecodeAccessToken(

                  data.token

                );

                sessionStorage.setItem('userData', user);

                console.log(user);

                this.router.navigate(['/home']);

              } else {

                this.loaderService.display(false);

                this.loginMessage = data.msg;

                this.router.navigate(['/home']);

              }

            },

            (error) => {

              this.loaderService.display(false);

              this.loginMessage = error.error.error_description;

              console.log(error);

            }

          );

      } else {

        this.loaderService.display(false);

      }

    } catch (error) {

      this.loaderService.display(false);

    }

  }

The output produced by the Console.log(user) in above method is

{sub: "[email protected]", Roles: Array(1), exp: 1596814970, iat: 1596796970}
   Roles: ["System-Admin"]
  exp: 1596814970
   iat: 1596796970
  sub: "[email protected]"
   __proto__: Object

userInfo: any = sessionStorage.getItem('userData'); So when i try to console.log(userInfo) i am getting [object Object] So how to extract sub and Role from this.

Upvotes: 0

Views: 9137

Answers (4)

user13258211
user13258211

Reputation:

Sessionstorage (aswell as localstorage) stores key/value pairs of strings(!), which means you can't simply store objects there and retrieve them like you would for example with an array.

To store objects in either storage:

sessionStorage.setItem('object', JSON.stringify(obj))

-> this converts your object to a JSON string(!) and stores it under the key object.

To retrieve it back you need to parse the JSON string back to a object so you can access the properties as usual.

var jsonStringObj = sessionStorage.getItem('object'); // This is the json string we stored

var obj = JSON.parse(jsonStringOBJ); // this is your object

console.log(obj.department); // access properties as usual

 

Upvotes: 1

Asaf
Asaf

Reputation: 1564

You forgot JSON.stringify

Try:

sessionStorage.setItem('userData',JSON.stringify(user))

And then just:

JSON.parse(sessionStorage.getItem('userData'))

Upvotes: 2

Shivam
Shivam

Reputation: 95

this one working for me

  department = JSON.parse(window.sessionStorage.getItem('empDetails')).department;
  
  constructor() { }

  ngOnInit() {
    console.log(this.department);
  }

Upvotes: 0

Shivam
Shivam

Reputation: 95

Try to parse it

(JSON.parse(window.sessionStorage.getItem('empDetails')).id)

Upvotes: 0

Related Questions