TypeError:无法读取未定义的属性'length'(角度8)(标题)

问题描述 投票:0回答:1

尝试注销时出现此错误。

`core.js:6014 ERROR TypeError: Cannot read property 'length' of undefined
    at http.js:165
    at Array.forEach (<anonymous>)
    at HttpHeaders.lazyInit (http.js:153)
    at HttpHeaders.init (http.js:274)
    at HttpHeaders.forEach (http.js:376)
    at Observable._subscribe (http.js:2342)
    at Observable._trySubscribe (Observable.js:42)
    at Observable.subscribe (Observable.js:28)
    at subscribeTo.js:20
    at subscribeToResult (subscribeToResult.js:7)`

但是当我从邮递员发送HTTP请求时,代码起作用了。 I successfully logged out when I used the postman

我的认证服务

`logout(user){
    let headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': this.token }); //this.token is the value of token(eyJhbGciOiJIUzI1NiIsInR5cCI6IkpX...)
  let options = { headers: headers };
    localStorage.clear()
    this.token=null;
    return this.http.post<response>('http://localhost:3000/users/logout',null,options).pipe(map(res=>{
      console.log(res)
       return res
    }))}

`这是我订阅此http请求的代码:

 onLogout(){

    this.authService.logout(this.dataService.storingToken).subscribe(res=>{
      console.log(res)
      if(res.status===true){
      this.router.navigate(["/login"])

 }else{
        console.log("some error has occurred")
      }
    })
  }
angular authentication jwt observable mean-stack
1个回答
0
投票

该错误引用了http.js第165行,它是以下代码(至少在Angular 8中:)]

© www.soinside.com 2019 - 2024. All rights reserved.