我想修改这个函数,在单独的请求中发送这两个文件id:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body);
})
);
我应该使用平面地图吗?
您可以像这样从一个管道中分离请求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return [
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
]
}),
mergeAll(),
);
正确的操作符取决于您是希望并行还是连续发送两个请求。
如果你已经有了take(1)
,那么你可以同时使用switchMap
或mergeMap
,因为它总是只排放一次,因此在这种情况下没有关系。
并行发送请求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return forkJoin([
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
]);
}),
);
按顺序发送请求:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return concat(
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
);
}),
);