javascript - ionic 2 local storage unable to set retrieved value to variable -


i trying set value retrieved .get function variable declared outside unable so.

var dt; //retrieve this.local.get('didtutorial').then((value) => {   alert(value);    dt = value; })  console.log("local storage value: "+dt); 

i'm able "true" alert, getting "undefined" console.log printing outside of function.

enter image description here enter image description here

one workaround can put remaining codes ".then function" , messy.

update(solution):

as per ionic api (http://ionicframework.com/docs/v2/api/platform/storage/localstorage/) , use .get retrieve values.

since using promises has it's own limitations, using following:

 constructor(navcontroller) {      this.navcontroller = navcontroller;      this.local = new storage(localstorage);  } 

and getitem function,

localstorage.getitem('didtutorial') 

you able retrieve without having put callback method.

reading localstorage wrapper in case asynchronous, means callback passed this.local.get gets called after call console.log. try placing console.log inside callback; should work then:

// retrieve this.local.get('didtutorial').then((value) => {   alert(value)   var dt = value   console.log("local storage value:", dt) }) 

also, you'll notice changed console.log call arguments. that's because console.log accepts 1 or more parameters, , formats them more nicely when pass them in instead of concatenating them. pro tip.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -