ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Ionic2 - I can’t retrieve the localStorage value

Open HamzaLJ opened this issue 10 years ago • 2 comments

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

For your information:

I used the default functions localStorage.getItem() and localStorage.setItem(), they work fine. I believe it's and Ionic2 bug! So please check it out!

Your system information:

Cordova CLI: Not installed Ionic Framework Version: 2.0.0-beta.4 Ionic CLI Version: 2.0.0-beta.24 Ionic App Lib Version: 2.0.0-beta.14 ios-deploy version: Not installed ios-sim version: Not installed OS: Mac OS X El Capitan Node Version: v5.9.0 Xcode version: Xcode 7.2 Build version 7C68

HamzaLJ avatar May 15 '16 13:05 HamzaLJ

Hi @HamzaLJ the .get() method returns a _promise_ because it's an async call, if I'm not mistaken. so in order to retrieve the value and console log it, you'd have to do something like -

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

Please read the very well written docs regarding localstorage here - http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/#get

foysalit avatar May 15 '16 18:05 foysalit

Hello! Thanks for opening an issue with us! As this seems like more of a support question i will urge that you ask this question on our forum (forum.ionicframework.com). Thanks for using Ionic!

jgw96 avatar May 16 '16 15:05 jgw96