如何检查 .pipe(map) Rxjs 操作符中的空响应?

Cha*_*sia 0 javascript observable rxjs typescript angular

getDetailsById(eId: number): Observable<Details> {
  return this.api.post('detailsapi', cacheOptions, { Id: eId })
    .pipe(
      map((eDetails: any) => ({
        id: eDetails.eId,
        subDetails: this.mapSubDetails(eDetails.eSubDetails || [])
      })),
      catchError((err) => {
        const template: Template = {   
          id: 0,
          subEquipment: []
        };
        return Observable.throw(err)
      })
    );
}
Run Code Online (Sandbox Code Playgroud)

我想检查上面的代码是否eDetails为空。如果为空,那么我想停止 Observable.forkJoin.

nir*_*aft 6

您可以filterpipe

.pipe(filter(isNotNull))
Run Code Online (Sandbox Code Playgroud)

并使用 typeguard 相同:

export function isNotNull<T>(value: T): value is NonNullable<T> {
  return value != null;
}
Run Code Online (Sandbox Code Playgroud)