-2

In my method I need to return "this.profile". But I can't use this.profile outside my profileObservable

  profileObservable: Observable<ProfileModel>;
  profile: ProfileModel;

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<ProfileModel> {
      this.store$.dispatch(new ProfileFeatureStoreActions.GetProfile());

      this.profileObservable = this.store$.pipe(
        select(
          ProfileFeatureStoreSelectors.selectProfile
        ),
        filter(profile => !!profile)
      );

      this.profileObservable.subscribe(profile => {
        this.profile = profile;
        console.log(this.profile) // here is defined

      });

      return this.profile; //it's undefined

  }

If you have the solution thank you so much

  • 1
    Possible duplicate of [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – Igor Jun 03 '19 at 17:55
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/1260204) – Igor Jun 03 '19 at 17:55

3 Answers3

0

You can not return subscribe. Your resolve return Observable so you must return Observable value.

For example:

return this.profileObservable;
0

You have to return return this.profileObservable; then you can subscribe to it. and another thing is why do you need this return this.profile; //it's undefined you can access this.profile anywhere in component as it's global variable.

indrajeet
  • 837
  • 10
  • 17
0

create a method inside your class like this :

get myProfile() {
     return this.profile;
  }

your profile value can now be accessed by calling the method "myProfile()".

another option would be to leverage the angular behaviour subject which you can subscribe to so as to get the last emitted value like:

step 1. register your behaviour subject just after your class properties as shown :

profileSubject: BehaviorSubject<ProfileModel> = new BehaviorSubject();
profile$: Observable< ProfileModel > = this.profileSubject.asObservable();

step2. use the registered subject to emit your profile value :

this.profileObservable.subscribe(profile => {
   this.profileSubject.next(profile)
});

to access your profile value outside your subscription do like this

  this.profile$.subscribe(res => {
    // the res contains your body;
   });
Edward Mwaura
  • 120
  • 1
  • 9
  • We already have observable value. We do not have to pack it. Just return `this.profileObservable`. – Kamil Augustyniak Jun 03 '19 at 21:31
  • @kamil :by doing this (return this.profileObservable) you are returning the whole observable and not the value published. Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The subscribed consumer then receives notifications until the function completes, or until they unsubscribe. – Edward Mwaura Jun 04 '19 at 14:43