Reputation: 302
I am retriving some data from firebase via observable in valueChanges
method.
this.authService.getUser().subscribe(
user => {
this.studentDetailsService.readStudentDatabase( user.uid ).subscribe( temp => {
this.student = temp as StudentDetails ;
});
}
);
but i cant use this.student
objects out side the Observable like this
this.authService.getUser().subscribe(
user => {
this.studentDetailsService.readStudentDatabase( user.uid ).subscribe( temp => {
this.student = temp as StudentDetails ;
});
}
);
this.name = this.student.firstName;
when im doing this console shows this.name
is undefined.
how to solve this ?? how can i use those retrived values outside the observable ?
Upvotes: 1
Views: 415
Reputation: 16292
You can do something like this
studenDetails$ = this.authService.getUser().pipe(
switchMap(user =>
this.studentDetailsService.readStudentDatabase(user.id)));
Then you can output the student details in your template using the async pipe rather than using subscribe.
{{ studenDetails$ | async | json }}
You can do something like the following, but I would recommend using using the above solution instead
this.authService.getUser().pipe(
switchMap(user =>
this.studentDetailsService.readStudentDatabase(user.id).pipe(
tap(student => this.student = student),
map(student => student.firstName),
tap(firstName => this.firstName = firstName))).subscribe();
This will set the class field firstName
to the students first name.
Upvotes: 1