我有问题如何使用2个不同的Api提交。当第一个Api返回成功,然后转到第二个Api。第一个Api成功,但是第二个Api失败,因为数据变得不确定。
这是我的代码
public signupform(userData: SignupRequest): Observable <any> {
return this.http.post('api/createorganisation').pipe(tap( // Log the result or error
data => {
if (data.status['message'] === 'Success.') {
return this.http.post('api/createuser');
}
}
));
}
希望大家能有所帮助
要HttpClient
像执行操作一样传递不同的请求,应考虑使用pipe运算符switchMap
。此外,if
由于您已经处于的成功回调中,因此我不确定您是否需要声明Observable
。
您可以使用如下所示的内容:
public signupform(userData: SignupRequest): Observable<any> {
return this.http.post('api/createorganisation')
.pipe(
switchMap(() => this.http.post('api/createuser'))
);
}
预订时,您将触发另一个查询,并获得第二个查询返回的结果。
希望能帮助到你 !
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句