Angular 我想在使用switchMap抛出错误时捕获错误

Angular 我想在使用switchMap抛出错误时捕获错误,angular,typescript,Angular,Typescript,使用switchMap()时,试图捕捉错误时遇到问题。 我想检查每个api的数据是否已经存在 这是我的密码 public signupform(userData: SignupRequest): Observable<any>{ return this.http.post('api/auth/createorganisation') .pipe (tap( // Log the result or error data => {

使用
switchMap()
时,试图捕捉错误时遇到问题。 我想检查每个api的数据是否已经存在

这是我的密码

public signupform(userData: SignupRequest): Observable<any>{

    return this.http.post('api/auth/createorganisation')
    .pipe
    (tap( // Log the result or error
      data => {
        if (data.status['message'] === 'Success.') {

      switchMap(() => this.http.post('api/auth/createuser')
       .pipe(
        catchError((error) => this.handleError(error))
        )
      )

        }else{
          throw new Error(data.status['message']);

        }
      }
    )
    )
  }

  error: boolean;
  private handleError(error: HttpErrorResponse) {

    this.error = true;
    console.log(error)
    return empty();
  }
公共注册表单(userData:SignupRequest):可观察{
返回此.http.post('api/auth/createOrganization')
管
(轻触(//记录结果或错误)
数据=>{
如果(数据状态['message']='Success'){
switchMap(()=>this.http.post('api/auth/createuser'))
.烟斗(
catchError((错误)=>this.handleError(错误))
)
)
}否则{
抛出新错误(data.status['message']);
}
}
)
)
}
错误:布尔;
私有句柄错误(错误:HttpErrorResponse){
this.error=true;
console.log(错误)
返回空();
}
希望你们都能帮忙


提前感谢

您正在使用
开关映射
内部
点击
,这是不正确的。您需要在
点击
操作符之前/之后移动
开关映射
(根据您的需要),如下所示(在此示例中,您不需要
点击
操作符btw):


public signupform(userData:SignupRequest):可观察的

要求您的IDE正确格式化和缩进代码,您将看到您在传递给tap()操作符的回调中调用了switchMap,而不是将其作为pipe()的参数调用。谢谢您的回答,但我在
data.status
中遇到了错误。它说属性“status”在类型“unknown”上不存在。和.
pipe
。它说类型“OperatorFunction”上不存在属性“pipe”。您可以执行类型转换,如
switchMap((数据)=>{…})
或任何您想要的响应类型,而不是
any
。请将第二个
管道
switchMap
替换为
http.post
(这是我的错)。它会出现另一个错误。属性“status”在类型“Object”上不存在。什么是
数据的控制台输出?
控制台的输出。日志(数据)
[Object Object]
public signupform(userData: SignupRequest): Observable<any>{

    return this.http.post('api/auth/createorganisation')
     .pipe(             
         switchMap((<any>data) => {
               if (data.status['message'] === 'Success.') {
                    return this.http.post('api/auth/createuser').pipe(
                      catchError((error) => this.handleError(error))
                    );
                }else{
                   throw new Error(data.status['message']);
               }
          })
       )
   )
}

error: boolean;
private handleError(error: HttpErrorResponse) {
  this.error = true;
  console.log(error)
  return empty();
}