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

I am trying to set the value retrieved from .get function into a variable declared outside but unable t

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
Add a ment  | 

1 Answer 1

Reset to default 6

Reading 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信