2

I have two observable working against http api.
One is asking for places, the second for events.

The one which asks for events returns a hash of:

place_id => events_arr

and my code shows

<ng-container *ngFor="let place of placesData">
  <ion-item>
    <h1>{{place.name}}</h1>

      <ion-slides>
        <ng-container *ngFor="let event of eventsData[place.id]">
          <ion-slide>
            <h1>{{event.title}}</h1>
          </ion-slide>
        </ng-container>
      </ion-slides>
  </ion-item>
</ng-container>

My problem is that placesData observable is faster than eventsData , so eventsData[place.id] raises the following exception:

TypeError: Cannot read property '12' of undefined

Any idea?

1
  • 2
    quick little hack <ion-slides *ngIf="eventsData">...</ion-slides> ? Commented Mar 1, 2017 at 12:29

1 Answer 1

4

Option 1 would be to use *ngIf as @mickdev already pointed via comment.

Option 2 would be to use forkJoin().

Observable.forkJoin(
   yourPlacesFunctionReturningAnObservable(),
   yourEventsFunctionReturningAnObservable())
.subscribe(result => {
   this.placesData = result[0];
   this.eventsData = result[1];
});

forkJoin will "wait" untill all observables are done.

docs: https://www.learnrxjs.io/operators/combination/forkjoin.html

so: https://stackoverflow.com/a/42373283/3631348

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.