【发布时间】:2018-08-01 08:33:32
【问题描述】:
我正在尝试使用 mergeMap 和 forkJoin 有条件地执行 5 个并行 http 请求。对我来说,只有第一个 api 被执行,其余的 api 没有触发。
我的函数将执行第一个 api,如果它返回状态为“NOT_FOUND”,那么我需要再执行 5 个 api 并需要返回最终结果。如果我得到的状态与“NOT_FOUND”不同,只需从第一个 http 请求返回 observable。我尝试了不同的选项,但似乎没有任何效果。在共享代码下方,我收到“ERROR TypeError: Unable to get property 'map' of undefined or null reference”
public myPostWrapper( url, body, flag): Observable<any> {
return this.http.post( url, body, { headers: this.headers } )
.map(( out: any ) => {
console.log( 'out', JSON.stringify( out ) );
this.result = out;
} )
.mergeMap(( response: any ) => {
console.log( 'response-inside', JSON.stringify( response ) );
if ( this.result.status === 'NOT_FOUND' ) {
return Observable.forkJoin(
response.map(( resp: any ) => {
return this.http.post(url, body, { headers: this.headers })
.map(( res: any ) => {
const section: any = res;
return section;
} );
} ),
response.map(( resp: any ) => {
return this.http.post(url, body, { headers: this.headers })
.map(( res: any ) => {
const section: any = res;
return section;
} );
} )
);
} else {
return Observable.of( this.result );
}
} )
.catch(( error: any ) => Observable.throw( error || 'Server error' )
);
}
任何帮助/指导将不胜感激。注意我使用 Rxjs 5.52 和 Angular 5
【问题讨论】:
标签: angular http observable rxjs5 fork-join