Hamza L.
Hamza L.

Reputation: 1823

JavaScript/Ionic2 - Why I can't retrieve the localStorage value?

I am trying to retrieve the localStorage value; however, I can't get any value and my log looks like the following:

log is: {}

This is my code so far:

import {Storage, LocalStorage} from 'ionic-angular';
//I am storing here
this.local = new Storage(LocalStorage);
this.local.set('options', 'op2'); 
//In the chrome developer tools, I can see that the value was stored successfully in localStorage

//I can't see anything, when trying to retrieve the stored value
console.log('log is : '+ JSON.stringify(this.local.get('options')));

Upvotes: 0

Views: 355

Answers (2)

Piotr Galas
Piotr Galas

Reputation: 4776

You can read value from localStorage in this way:

   this.local.get('options').then(value => console.log(value))

Upvotes: 1

Manu Valdés
Manu Valdés

Reputation: 2372

If you read the documentation that I already provided in your other question here , you'll find that set doesn't resolve immediately and instead returns a promise. If you try to get immediately instead of waiting for the Promise resolution the most likely outcome is that the storage is still empty.

Upvotes: 1

Related Questions