node.js - Angular 6 错误类型错误 : Cannot read property 'navigate' of undefined

标签 node.js angular typescript rxjs

在我的错误处理程序中,当遇到错误时,我尝试导航到另一个页面。

我尝试获取项目列表,如果失败我想导航到“./error401”。我一直在尝试这样做:

用户表组件:

export class UserDataSource extends DataSource<any>{


  constructor(private authservice: AuthService) {
    super();
  }
  connect(): any {
    return this.authservice.GetInstallation();
  }

  disconnect() { }
}

这个调用可能有点奇怪。它的作用是根据 token 调用其他人查看我拥有的 ID。然后在下一次调用中使用该 ID

auth.service

  GetServiceProviderId(): Observable<Info> {
    return this.http.get<Info>(this.rooturl + 'info', { headers: this.reqHeader });
  }

  GetInstallation(): Observable<Installation[]> {
    return this.GetServiceProviderId().pipe(
      flatMap(info => {
        return this.http.get<Installation[]>
          (this.rooturl +
          "installation/?serviceproviderid=" +
          info.ServiceProviderId,
          { headers: this.reqHeader })
      }
    ), catchError(this.myerrorhandle.handleError))
  }
}

myerrorHandle

@Injectable({
  providedIn: 'root'
})
export class myerrorHandle {

  constructor(public router: Router) { }

  handleError(errorResponse: HttpErrorResponse) {
    switch (errorResponse.status) {
      case 401: {
        this.router.navigate(["./error401"])
        break;
      }
      default: {
        console.log("todo");
        break;
      }
    }
    return throwError('an error was thrown');
  }
}

这是我想导航到“./error401”但得到

errror ERROR TypeError: Cannot read property 'navigate' of undefined

这是完整的错误日志:

ERROR TypeError: Cannot read property 'navigate' of undefined at CatchSubscriber.push../src/app/myerrorHandle.ts.myerrorHandle.handleError [as selector] (myerrorHandle.ts:18) at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:33) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/OuterSubscriber.js.OuterSubscriber.notifyError (OuterSubscriber.js:13) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._error (InnerSubscriber.js:18)

最佳答案

正如@user184994所说:

Change catchError to catchError((err) => this.myerrorhandle.handleError(err))) otherwise (without arrow functions) the context of this is lost

关于node.js - Angular 6 错误类型错误 : Cannot read property 'navigate' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52112949/

相关文章:

tsconfig.json 中的 typescript outDir 设置不起作用

typescript - Electron-forge:如何将入口点设置为 url 而不是静态 html 文件

javascript - NodeJS中mongo的limit和skip方法约定

node.js - 处理可组合中间件中的 Express-JWT 错误

javascript - 从双重嵌套函数返回值

angular - 无法读取未定义的属性 'unsubscribe'(rxJS - 订阅)

javascript - Angular 2 : Event Emitter not working properly

Angular 2 事件发射器与主题

typescript - 如何在 TypeScript 中正确使用函数的构造签名

angularjs - 从 Nodejs 向 Nodejs 发送请求