Angular 9 TypeError:无法读取未定义的属性“ subscribe”

布莱恩·范德·斯特尔

我要完成的工作是查看用户会话是否已经存在,是否已经登录。但是我在如何正确执行此操作方面遇到问题。它最有可能与代码执行顺序有关,但我不知道该如何解决。也许异步?

获得:

TypeError:无法读取未定义的属性“ subscribe”

Sidebar.component.ts:

ngOnInit(): void {
this.authService.isLoggedIn.subscribe(res => {
  if (res){
    this.isLoggedIn = true;
  } else {
   
    this.isLoggedIn = false;
  }
});
}

auth.service.ts:

      get isLoggedIn(): Observable<boolean> {
    const authToken = this.cookieService.get('cookiehere');
    // console.log(authToken);
    if (authToken) {
      this.getUserBySession(authToken).subscribe(res => {
        console.log(res);

        return of(true);
      }, error => {
        return of(false);
      });
    }
    if (!authToken){
      console.log('No token found!');
      return of(false);
    }
  }


  getUserBySession(session) {
    return this.http.get(`${this.apiPrefix}/auth/get_user_by_session?session=${session}`, { headers: this.headers })
      .pipe(map((res: any) => {
        if (res.user) {
          this.currentUser = res.user;
          return res;
        } else {
          return res;
        }}));
      }
Xetera

您在此处订阅了一个可观察的对象,但没有从getter返回一个可观察的对象。return of(true)你从内必须有回报subscribe的回调,而不是吸气。您将不得不更改它以从getUserBySession函数返回原始可观察到的

get isLoggedIn(): Observable<boolean> {
  const authToken = this.cookieService.get('vmkr');
  if (authToken) {
    return this.getUserBySession(authToken).pipe(
      map(() => true),
      catchError(() => of(false))
    );
  } else {
    console.log('No token found!');
    return of(false);
  }
}

尽管我想象这里的吸气剂可能会导致多个订阅的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular 9-错误TypeError:无法读取未定义的属性“名称”

来自分类Dev

http / rxjs中的Angular 2重定向捕获回调导致TypeError:无法读取未定义的属性'subscribe'

来自分类Dev

无法读取Angular 2中的嵌套调用中未定义的属性“ subscribe”

来自分类Dev

angular2指令“无法读取未定义的属性” subscribe”,带有输出元数据

来自分类Dev

Angular TypeError:无法读取未定义的属性“ then”

来自分类Dev

TypeError:无法读取未定义<Angular 8>的属性'map'

来自分类Dev

Angular:无法读取未定义TypeError的属性“ routeConfig”

来自分类Dev

Angular Material TypeError:无法读取未定义的属性“ id”

来自分类Dev

Angular / Karma TypeError:无法读取未定义的属性“ offsetTop”

来自分类Dev

Angular TypeError无法读取未定义的属性“ then”

来自分类Dev

NativeScript/Angular - TypeError:无法读取未定义的属性“符号”

来自分类Dev

Angular - TypeError:无法读取未定义的属性“推送”

来自分类Dev

Angular - TypeError:无法读取未定义的属性“订阅”

来自分类Dev

Angular 5:TypeError:无法读取未定义的属性“密码”

来自分类Dev

Angular 6:TypeError:无法读取未定义的属性“值”

来自分类Dev

Angular 9:由于“无法读取未定义的属性'$ owl'属性”错误而未呈现ngx-owl-carouel

来自分类Dev

例外:TypeError:升级后无法读取未定义的属性“ subscribe”

来自分类Dev

错误TypeError:无法读取POST HTTP调用上未定义的属性“ subscribe”-角度[8]

来自分类Dev

例外:TypeError:升级后无法读取未定义的属性“ subscribe”

来自分类Dev

Angular无法读取未定义的属性

来自分类Dev

TypeError:无法从未定义中读取属性“值”。(第9行,文件“代码”)

来自分类Dev

TypeError:添加自定义验证器时,无法读取未定义Angular的属性'indexOf'

来自分类Dev

Angular2-TypeError:无法读取(Typescript)中未定义的属性'Id'

来自分类Dev

Angular 2异常:TypeError:无法读取未定义的属性“ annotations”

来自分类Dev

Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

来自分类Dev

TypeError:无法读取未定义的Angular.js的属性“ get”

来自分类Dev

NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

来自分类Dev

Angular 8:ERROR TypeError:无法读取未定义的属性“无效”

来自分类Dev

Angular 8错误TypeError:无法读取未定义的属性“名称”

Related 相关文章

  1. 1

    Angular 9-错误TypeError:无法读取未定义的属性“名称”

  2. 2

    http / rxjs中的Angular 2重定向捕获回调导致TypeError:无法读取未定义的属性'subscribe'

  3. 3

    无法读取Angular 2中的嵌套调用中未定义的属性“ subscribe”

  4. 4

    angular2指令“无法读取未定义的属性” subscribe”,带有输出元数据

  5. 5

    Angular TypeError:无法读取未定义的属性“ then”

  6. 6

    TypeError:无法读取未定义<Angular 8>的属性'map'

  7. 7

    Angular:无法读取未定义TypeError的属性“ routeConfig”

  8. 8

    Angular Material TypeError:无法读取未定义的属性“ id”

  9. 9

    Angular / Karma TypeError:无法读取未定义的属性“ offsetTop”

  10. 10

    Angular TypeError无法读取未定义的属性“ then”

  11. 11

    NativeScript/Angular - TypeError:无法读取未定义的属性“符号”

  12. 12

    Angular - TypeError:无法读取未定义的属性“推送”

  13. 13

    Angular - TypeError:无法读取未定义的属性“订阅”

  14. 14

    Angular 5:TypeError:无法读取未定义的属性“密码”

  15. 15

    Angular 6:TypeError:无法读取未定义的属性“值”

  16. 16

    Angular 9:由于“无法读取未定义的属性'$ owl'属性”错误而未呈现ngx-owl-carouel

  17. 17

    例外:TypeError:升级后无法读取未定义的属性“ subscribe”

  18. 18

    错误TypeError:无法读取POST HTTP调用上未定义的属性“ subscribe”-角度[8]

  19. 19

    例外:TypeError:升级后无法读取未定义的属性“ subscribe”

  20. 20

    Angular无法读取未定义的属性

  21. 21

    TypeError:无法从未定义中读取属性“值”。(第9行,文件“代码”)

  22. 22

    TypeError:添加自定义验证器时,无法读取未定义Angular的属性'indexOf'

  23. 23

    Angular2-TypeError:无法读取(Typescript)中未定义的属性'Id'

  24. 24

    Angular 2异常:TypeError:无法读取未定义的属性“ annotations”

  25. 25

    Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

  26. 26

    TypeError:无法读取未定义的Angular.js的属性“ get”

  27. 27

    NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

  28. 28

    Angular 8:ERROR TypeError:无法读取未定义的属性“无效”

  29. 29

    Angular 8错误TypeError:无法读取未定义的属性“名称”

热门标签

归档