1

I'm learning Ionic 2. I've stored some data in ionic local storage inside a SomeComponent and retriving the same in UserService to call backend but storage.get() method returns Promise so i can't get data out of it as i learnt about promises. Can anybody assist me better way to get this job done? Thanks if any.

here is my code.

export class SomeComponent {

 user: User;
 usename: string;
 password: string;

   constructor(
    public navParams: NavParams,
    public navController: NavController, 
    public loginService: LoginSevice,
    public storage: Storage) {

   }

    login() {
        this.loginService.login(this.username, this.password).subscribe( response => {
          this.user = response;
          this.storage.set('userId', this.user.userId);
          this.storage.set( 'authcode', this.user.authCode);
        });
      }
}

And the service is

    export class UserService {

      userId: string;
      constructor(public http: Http, public storage: Storage){}

      fetchUserInfo(): Observable<User> { 

          this.storage.get('userId').then((val) => {
            console.log(val); // available only inside then() method
            this.userId = val; 
          });

          let user = btoa(this.userId); // userId is undefined
          return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: 
               this.headers})
             .map(res =>res.json());
         }
      }
   }
sebaferreras
  • 44,206
  • 11
  • 116
  • 134
Gavishiddappa Gadagi
  • 1,120
  • 1
  • 16
  • 34

2 Answers2

2

One way to solve this scenario would be like this:

export class UserService {

    userId: string;
    constructor(public http: Http, public storage: Storage){}

    fetchUserInfo(): Observable<User> { 

        return Observable
            .fromPromise(this.storage.get('userId'))
            .flatMap(userId => 
                this.userId = val;

                let user = btoa(this.userId);
                return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: this.headers})
                           .map(res =>res.json());
    }
}

This way, we're first creating an observable with the promise that will return the value (userId) from the storage, and then using that userId we're making the http request (that will return the Observable<User>). You can find more information about the flatmap operator here.

sebaferreras
  • 44,206
  • 11
  • 116
  • 134
0

I tried with mergeMAp this also works perfectly. I don't know the difference of using both flatMap and mergMap but it works.

export class UserService {
    userId: string;
    constructor(public http: Http, public storage: Storage){}
    fetchUserInfo(): Observable<User> { 
    return Observable
          .fromPromise(this.storage.get('userId')).mergeMap(userId => 
              this.userId = val;
              let user = btoa(this.userId);
              return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: this.headers})
                               .map(res =>res.json());
    }
}
Gavishiddappa Gadagi
  • 1,120
  • 1
  • 16
  • 34