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());
}
}
}