【发布时间】:2019-12-31 04:35:09
【问题描述】:
我需要将带有两个 api 的单个表单发送到后端。我展示了一些代码作为参考。我只是展示有问题的部分。 我需要当用户点击提交表单时,首先它必须检查组织 ID 是否已经存在。如果存在则抛出错误消息。我的问题是当组织 ID 已经存在时,它不会抛出错误消息。如何实现它
服务
public signupform(userData: SignupRequest): Observable<any>{
let userid = userData.userId;
return this.http.post('http://localhost:9080/project/api/auth/createorganisation')
.pipe
(tap( // Log the result or error
data => {
if (data.status['message'] === 'Success.') {
switchMap(() => this.http.post('http://localhost:9080/project/api/auth/createuser')
.pipe(catchError((error) => {return empty()}))
)
}else{
throw new Error(data.status['message']);
}
},
)
)
}
【问题讨论】:
-
如果组织 ID 已经存在,您的
POST /api/auth/createorganisation是否会抛出错误消息? -
No.. 当组织 ID 已经存在时,它不会抛出任何错误消息
-
仅
post /api/auth/createuser抛出错误信息 -
如果
POST /api/auth/createorganisation确实抛出了错误,那么您可能会更优雅地处理这个问题。 -
您是从哪里获得该组织 ID 的?从下拉列表中?如果是,那么您可以在从下拉列表中选择后点击组织检查服务。无需检查表单提交。
标签: angular typescript api observable