-1

I have a angular 8 application and a service, like this:

export class ProfileUserService {
  user$ = this.authService.loginStatus().pipe(take(1));

  constructor(private profileService: ProfileService, private authService: AuthService) {}

  getProfile(): Observable<ProfileApi> {
    return this.user$.pipe(mergeMap(({ profile }) => this.profileService.get(profile.participant)));
  }
}

And I have a component where I use the service where I call the method, like this:

export class SettingsAccountComponent extends FormCanDeactivate implements OnInit, OnDestroy {

 constructor(
    private profileUserService: ProfileUserService){}

 ngOnInit() {
    this.innerWidth = window.innerWidth;
    this.profileSubscription = this.profileUserService.getProfile().subscribe((profile: ProfileApi) => {
      this.profile = profile;
      this.deletePicture = false;
      this.buildForm();
    });
  }




}

But I want to call directly in the component SettingsAccountComponent : this service:

private profileService: ProfileService

But the problem is this:

 user$ = this.authService.loginStatus().pipe(take(1));

Because I need that for getting the participantId. But so my question is, how to combine the ProfileService, like this

 this.profileSubscription = this.profileService.get().subscribe((profile: ProfileApi) => {
      this.profile = profile;
      this.deletePicture = false;
      this.buildForm();
    });

witht the:

 user$ = this.authService.loginStatus().pipe(take(1));

because now in the get() method it expecs a ParticipantId

So what I have to change?

Thank you

1 Answers1

0

I think a switchMap can help you.

Try:

import { switchMap } from 'rxjs/operators';
...
this.profileSubscription = this.profileService.user$.pipe(
  switchMap(({ profile }) => this.profileService.get(profile.participant))
).subscribe((profile: profileAPI) => {
  this.profile = profile;
  this.deletePicture = false;
  this.buildForm();
});

I see you've already done mergeMap in your service, switchMap is very similar.

AliF50
  • 16,947
  • 1
  • 21
  • 37
  • @can we go to chat? I think I can better explain. –  Mar 31 '20 at 20:33
  • The between service: ProfileUserService has to be gone. I want to use the service: ProfileService directly in the component. But I need a ParticipantId from this: user$ = this.authService.loginStatus().pipe(take(1)); –  Mar 31 '20 at 20:34
  • I voted because I am glad it worked. Oh, the `-1` was not me. – AliF50 Mar 31 '20 at 21:43
  • Of course not you. But do you understand why people did not positive vote on this?? Don't understand –  Mar 31 '20 at 21:45
  • It happens to me too, don't let it get to you. There will always be haters :). – AliF50 Mar 31 '20 at 21:47