I am trying to set the value retrieved from .get function into a variable declared outside but unable to do so.
var dt;
//retrieve
this.local.get('didTutorial').then((value) => {
alert(value);
dt = value;
})
console.log("Local Storage value: "+dt);
I'm able to get "true" for the alert, but getting "undefined" for the console.log that is printing outside of the function.
One workaround is that I can put all my remaining codes into the ".then function" , but that would be very messy.
Update(Solution):
As per ionic api (/) , they use .get to retrieve values.
Since using promises has it's own limitations, by using the following:
constructor(navController) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
and getItem function,
localStorage.getItem('didTutorial')
You will be able to retrieve it without having to put everything into the callback method.
I am trying to set the value retrieved from .get function into a variable declared outside but unable to do so.
var dt;
//retrieve
this.local.get('didTutorial').then((value) => {
alert(value);
dt = value;
})
console.log("Local Storage value: "+dt);
I'm able to get "true" for the alert, but getting "undefined" for the console.log that is printing outside of the function.
One workaround is that I can put all my remaining codes into the ".then function" , but that would be very messy.
Update(Solution):
As per ionic api (http://ionicframework./docs/v2/api/platform/storage/LocalStorage/) , they use .get to retrieve values.
Since using promises has it's own limitations, by using the following:
constructor(navController) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
and getItem function,
localStorage.getItem('didTutorial')
You will be able to retrieve it without having to put everything into the callback method.
Share Improve this question edited Apr 25, 2016 at 7:11 Gene asked Apr 25, 2016 at 3:35 GeneGene 2,2184 gold badges31 silver badges52 bronze badges 2-
Hey, this evaluates to:
Error TS2339: Property 'getItem' does not exist on type 'Storage'.
– Thomas Modeneis Commented Aug 26, 2016 at 12:32 - @ThomasModeneis I might not be sure as this is for .beta5 and lower. – Gene Commented Sep 7, 2016 at 8:39
1 Answer
Reset to default 6Reading from your localStorage wrapper in this case is asynchronous, which means that the callback passed to this.local.get
gets called after your call to console.log
. Try placing console.log
inside your callback; it 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 that I changed your console.log
call arguments. That's because console.log
accepts 1 or more parameters, and formats them much more nicely when you pass them in instead of concatenating them. Just a pro tip.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262338a4619266.html
评论列表(0条)