【问题标题】:Combine Two API To send Data To Back-End结合两个 API 将数据发送到后端
【发布时间】: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


【解决方案1】:
return this.http.post('http://localhost:9080/project/api/auth/createorganisation')
    .pipe(switchMap((data:any)=>{
          //you get the result
         if (data.status['message'] === 'Success.')
             return this.http.post('http://localhost:9080/project/api/auth/createuser')
         else 
           return throwError(data.status['message'])
    }))

好吧,如果创建用户可以返回 data.status['message'] === 'Success.'不管是不是,我们可以再做一个switchmap

return this.http.post('http://localhost:9080/project/api/auth/createorganisation')
     .pipe(switchMap((data:any)=>{
          //you get the result
         if (data.status['message'] === 'Success.')
             return this.http.post('http://localhost:9080/project/api/auth/createuser')
         .pipe(switchMap((data:any)=>{
             if (data.status['message'] === 'Success.')
                 return of(data)
             else
                 return throwError(data.status['message'])
         }))
         else 
           return throwError(data.status['message'])
    }))

看到我们使用了 throwError (a 'rxjs' create observable),而不是新的错误

一般我们都喜欢订阅

this.service.signupform(data).susbcribe(
    (res)=>{console.log(res)},
    (error)=>{console.log(error)}
)

但您可以采取另一种方法,您所服务的总是返回一个值

return this.http.post('http://localhost:9080/project/api/auth/createorganisation')
     .pipe(switchMap((data:any)=>{
          //you get the result
         if (data.status['message'] === 'Success.')
             return this.http.post('http://localhost:9080/project/api/auth/createuser')
         .pipe(map((data:any)=>{
             if (!data.status['message'] === 'Success.')
                 return {error:data.status['message']}
         }))
         else 
           return of({error:data.status['message']})
    }),catchError((error)=>{
        return of({error:error})
    })

所以,你喜欢订阅

this.service.signupform(data).susbcribe(
    (res)=>{
      if (res.error){
         ....
      }
      else
        console.log(res)}
)

【讨论】:

  • 非常感谢您的回答...如果post http://localhost:9080/project/api/auth/createuser 已经有用户ID,它也会抛出错误消息吗?
  • 我更新了我的答案,如果有sintax错误很抱歉,-我很着急-
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多