1

I wanted to use a value from a subscribe method in another one, but it gives me undefined because it is not async. Is there a method to use those values together? I want to use this.internships another time in the following subscribe method but it becomes undefined. Thank you for helping!

Code:

ngOnInit(): void {
        this._internshipAssignmentService.getInternshipAssignments()
          .subscribe(internships => { this.internships = internships; <---- value which gives an object
          this.internshipsHelper = internships; console.log(this.internships)},
            error => this.msgs.push({
              severity: 'error',
              summary: 'Error',
              detail: 'Er is een onverwachte fout opgetreden.'
            }));
        this.sub = this._route.params.subscribe(
          params => {
            let id = +params['id'];
            this._internshipAssignmentService.getAllFavorites()
              .subscribe(f => {
                this.favorites = f;
                this.favorite = this.getFavoritesFromIdStudent(1);
                console.log(this.internships); <----- value which gives undefined 
                this.getFavorites(this.favorite);
              });
          }
      );
    }
n00dl3
  • 21,213
  • 7
  • 66
  • 76
BrianM
  • 951
  • 2
  • 12
  • 29
  • So you want to combine the results of two observables? Use `Observable.combineLatest`. – jonrsharpe May 07 '17 at 13:11
  • I just need the values of the first one in the second one yes. I'm looking at the combineLatest method right now but it is hard to find with two subscribe methods. @jonrsharpe – BrianM May 07 '17 at 13:43
  • What do you mean *"with two subscribe methods"*? It's just `Observable.combineLatest(this._internshipAssignmentService.getInternshipAssignments(), this.router.params, (assignments, params) => { ... });` – jonrsharpe May 07 '17 at 13:46

1 Answers1

0

this.internships is undefined because these calls are asynchronous, you can get more informations here.

Also note that you are using multiple subscription, which is not a good practice, you should combine your observable using some operators like switchMap map,pluck, etc.

ngOnInit(): void {
    this.sub = this._internshipAssignmentService.getInternshipAssignments().do((internships) => {
            this.internships = internships; // not needed if you just use it in next callbacks
            this.internshipsHelper = internships;
            console.log(this.internships)
        }).catch(error => {
            this.msgs.push({
                severity: 'error',
                summary: 'Error',
                detail: 'Er is een onverwachte fout opgetreden.'
            })
        }).switchMap(internships => this._route.params.pluck('id').switchMap(id => {
            return this._internshipAssignmentService.getAllFavorites().do(f => {
                this.favorites = f;
                this.favorite = this.getFavoritesFromIdStudent(1);
                this.getFavorites(this.favorite);
            })
        }))
        .subscribe();
}
n00dl3
  • 21,213
  • 7
  • 66
  • 76
  • Thank you, I am using angular2 for a project which is only once so I am really not great at it. The code you gave me works and now I know i cannot use multiple subscribes but use switchmaps. But it is working fine now , thank you ! It also took some time to understand your code because it is complicated if you dont know the functions that well – BrianM May 07 '17 at 17:39
  • Glad it helped. You can take a look at the [rxjs docs](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) to see what are the available operators. Also, take a look at the little tool on the home page "i ha ve one observable..." which can be useful to find the right operator for what you want to achieve. – n00dl3 May 07 '17 at 18:57