2017-05-15 4 views
-1

사용자 개체를 작성하기 위해 필요한 몇 가지 서비스 호출이 있습니다. 데이터 중 하나가 선택 사항이므로 항상 데이터를 반환하지는 않습니다.각도 rxjs Observable.forkJoin 404

아래 코드가 있지만, 옵션 API 호출로 404 오류가 발생하면 코드가 .map 함수로 변환되지 않습니다. 선택적 URL API 호출에서 catch를 치는 것을 볼 수 있지만 .map은 호출되지 않습니다. 404 응답을 반환하는 API와 함께 forkJoin을 사용할 수 있습니까?

return Observable.forkJoin([ 
     this.wmrk_http.get('required-url') 
      .map((res:Response) => <user> res.json()) 
      .catch((res:Response) => Observable.empty<user>()), 
     this.wmrk_http.get('required-url-2') 
      .map((res:Response) => <groups> res.json()) 
      .catch((res:Response) => Observable.empty<groups>()), 
     this.wmrk_http.get('optional-data-url') 
      .map((res:Response) => <userData> res.json()) 
      .catch((res:Response) => Observable.empty<userData>()),  
    ]) 
    .map((data: any[]) => { 
     ... 
    }); 
+0

이 *는 [mcve]와 함께 * "작동하지 않는"에 확장하십시오입니다 the working plunker

Observable.forkJoin([ this.http.get('https://api.github.com/users/karser') .map((res:Response) => res.json()) .catch((res:Response) => Observable.of(null)), this.http.get('https://api.github.com/fsdfsdf') .map((res:Response) => res.json()) .catch((res:Response) => Observable.of(null)), this.http.get('https://api.github.com/2') .map((res:Response) => res.json()) .catch((res:Response) => Observable.of(null)) ]) .map((data: any[]) => { console.log(data) }) .subscribe(); 

참조하십시오. – jonrsharpe

답변

0

forkJoinstops the chain 항목 중 하나의 값을 방출하지 않는 경우. 따라서 defaultIfEmpty를 사용하거나 observable.emptyobservable.of(null)으로 대체하십시오. 출력은

GET https://api.github.com/users/karser 200 (OK) 
GET https://api.github.com/fsdfsdf 404 (Not Found) 
GET https://api.github.com/2 404 (Not Found) 

[Object, null, null]