Ionic2 - 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')));

local.get returns a promise you need to handle. Try this.

this.local.get('options').then((options) => {
     console.log(JSON.stringify(options));
});
1 Like

is it possible to store this.local.get in a variable? because I tried it and it does not work!

I would recommend you to first read about Promises (e.g. this article or this one) and also about asynchronous JavaScript. Moreover when you’re using an API you should learn to use its docs. IMHO it’s much more difficult for the others to help you when you don’t understand some core concepts.

1 Like