2

i'm working on project update in which dashboard page is having more than 3, 000 lines of code.

i want to persist the previous working

so i want to convert this function

  returnUserDetails(){
       var userDetails = JSON.parse(localStorage.getItem("userData"));

       return userDetails ;      
     }

to this one but giving error of undefined return value

 returnUserDetails(){
    var userData;
    this.storage.get('userData').then((val) => {
         userData = JSON.parse(val);  
     });

   return userData; 
 }

Question: i cannot use global variable as this function used so many times. how can i return value correctly

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Goutham Ggs
  • 91
  • 14

2 Answers2

3

You can just do return the promise

 returnUserDetails(){   
    return  this.storage.get('userData').then((val) => {
          let userData = JSON.parse(val); 
          return userData;
     });
 }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

Since you're dealing with Async method you need to do it as shown below.

returnUserDetails(): Promise<any> {        
    return this.storage.get('userData').then((val) => {
         return val;  
     });      
 } 
Sampath
  • 63,341
  • 64
  • 307
  • 441