我遇到了如何使用 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
请求,您应该考虑使用管道运算符 switchMap
。此外,我不确定是否需要您的 if
语句,因为您已经在 Observable
的成功回调中。
你可以使用这样的东西:
public signupform(userData: SignupRequest): Observable<any> {
return this.http.post('api/createorganisation')
.pipe(
switchMap(() => this.http.post('api/createuser'))
);
}
订阅时,您将依次触发两个查询并获得第二个查询返回的结果。
希望对您有所帮助!
关于angular - 提交具有条件的两个不同 Api 的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56866010/