小编use*_*113的帖子

Angular:TypeError:订阅()时无法读取null的属性“长度”

在服务中发出 HTTP 请求时,我收到此响应:

TypeError: Cannot read property 'length' of null
    at eval (http.js:123)
    at Array.forEach (<anonymous>)
    at HttpHeaders.lazyInit (http.js:117)
    at HttpHeaders.init (http.js:265)
    at HttpHeaders.forEach (http.js:368)
    at Observable.eval [as _subscribe] (http.js:2172)
    at Observable.subscribe (Observable.js:162)
    at eval (subscribeToObservable.js:16)
    at subscribeToResult (subscribeToResult.js:6)
    at MergeMapSubscriber._innerSub (mergeMap.js:127)
ALERT!!!!! 
Run Code Online (Sandbox Code Playgroud)

我在加载组件时订阅了 HTTP 请求:

export class TasksComponent implements OnInit {
  currentUser:any;
  username:string=null;
  constructor(private usersService:UsersService) {}
  ngOnInit() {
    this.username=localStorage.getItem('currentUsername');
    console.log(this.username);
    this.usersService.getUserByUsername(this.username)
      .subscribe(data=>{
        console.log(data);
        this.currentUser=data;
      },err=>{
        console.log(err);
        console.log("ALERT!!!!! ");
      })
  }
}
Run Code Online (Sandbox Code Playgroud)

用户服务:

//for getting a user by its username …
Run Code Online (Sandbox Code Playgroud)

http angular-services angular

1
推荐指数
1
解决办法
5736
查看次数

标签 统计

angular ×1

angular-services ×1

http ×1