0

I had problems with my localforage, where localforage can't set a global variable in the success callback but when i console.log display showing values.

Localforage data:

Key: login
Value: [{"token": "bla bla bla", "member_id":1}]

Code

public login_data: any;
getMemberData() {
  localforage.getItem("login").then(function(value){
    this.login_data = value[0];
  });
  console.log(this.login_data);
} 

Output

undefined

All I want is to make it into a global variable. And I can use it easily, such as:

this.login_data.member_id
or
this.login_data.token

Some ways I have tried not correspond to my expectations. i have tried:

Assign value from successful promise resolve to external variable

how to assign the returned value of a promise to a variable?

setting a variable to get return from call back function using promise

Assign value from then function to a variable promise

[Note] I use Ionic 2, AngularJS 2, LocalForage

thanks

Community
  • 1
  • 1
nauval
  • 169
  • 1
  • 4
  • 13

1 Answers1

0

The promise is evaluated and then sets this.login_data = value[0]; .If you change your console output inside the promise see if it shows the output properly.

localforage.getItem("login").then(function(value){
this.login_data = value[0];
console.log(this.login_data);
 });

UPDATE -- please share output

ngOnInit() {
  this.login_data= "My login details";
  console.log('Init to empty : ' );
  console.log(this.login_data);
  console.log('--------');
  this.getMemberData();       
 }


getMemberData() {
 localforage.getItem("login").then(function(value){
 console.log(value[0]);
 console.log('My Init login data still is -');
 console.log(this.login_data);
 console.log('--------');
 this.login_data = value[0];
 console.log('After fetch -');
 console.log(this.login_data);
 console.log('--------');
 });  
} 
Searching
  • 2,269
  • 1
  • 17
  • 28
  • it displays an error: `Uncaught (in promise): TypeError: Cannot set property 'login_data' of null` – nauval Oct 14 '16 at 20:32
  • So you have your value correctly fetched, now its setting it against the login_data object. Correct ? Try setting`this.login_data = 'Login data'` manually, and display the output. See if its even accepting anything. Then check `value` that you fetch. Are you able to share that result ? – Searching Oct 14 '16 at 20:44
  • I think `value` or `value[0]` is null,may be because it doesn't exist or not reachable from the module.So i would step back to see how to fetch the item..and may be even check storage via browser devtools to see if the item exists. Call `getMemberData()` inside `ngOnInit()`.. – Searching Oct 14 '16 at 21:07
  • when I 'console.log (value [0])' it displays the correct result, I have tried to set the manual but did not change anything(same error), any idea? – nauval Oct 14 '16 at 21:09
  • Fair enough, atleaset we know that data is fetched correctly. Did you try this in `ngOnInit` ? and set `this.login_data = {}` in `ngOnInit`; its only declared as type `any` and not initialized. Hope you are following.. then call `getMemberdata()` after. – Searching Oct 14 '16 at 21:17
  • no changes, i add it before the `constructor` like this: – nauval Oct 14 '16 at 21:27
  • `ngOnInit() { this.login_data = {}; }` – nauval Oct 14 '16 at 21:27
  • Updated answer. – Searching Oct 14 '16 at 22:00