Javascript 无法读取属性';用户名';尝试访问id数据时,为null

Javascript 无法读取属性';用户名';尝试访问id数据时,为null,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,我想将用户数据传递到其配置文件。我从api获得完整的数据,我可以获取并在用户页面上显示它。但是,当我想在用户配置文件页面上显示来自用户的数据时,我会出现以下错误: TypeError:无法读取null的属性“username” 路由工作正常。但不知何故,我无法访问要存储在信息中的用户信息。我认为这行应该存储用户的数据: this.userService.getUserDetails(id).subscribe(result => { this.information = re

我想将用户数据传递到其配置文件。我从api获得完整的数据,我可以获取并在用户页面上显示它。但是,当我想在用户配置文件页面上显示来自用户的数据时,我会出现以下错误:

TypeError:无法读取null的属性“username”

路由工作正常。但不知何故,我无法访问要存储在
信息中的用户信息。我认为这行应该存储用户的数据:

this.userService.getUserDetails(id).subscribe(result => {
       this.information = result;
     });
我怎样才能解决这个问题? 这是我的密码:

用户服务

 // get a list of users
  getList(): Observable<any> {
    return this.http.get(`${this.url}/users`);
  }

 // get a user's profile
  getUserDetails(id): Observable<any> {
    return this.http.get(`${this.url}/users/?i=${id}`); // why add ?i
  }
profile.page.ts

information = null;
    ...
ngOnInit() {

         // Get the ID that was passed with the URL
         let id = this.activatedRoute.snapshot.paramMap.get('id');

         // Get the information from the API
         this.userService.getUserDetails(id).subscribe(result => {
           this.information = result;
         });
       }
profile.page.html

<ion-label text-wrap>
      <h6 class="subinfo">{{information.username}},&nbsp;22,&nbsp;3589</h6>
</ion-label>

{{information.username}},223589

您的组件以
信息=null开始
所以在
{{information.username}}
中出现此错误是正常的

快速解决方案是:

{{ information?.username }}

它修复了错误。但是现在我没有用户名,它只是空的,在你的SUBSCRIBE中有一个
console.log(result)
,你有一个值吗?是的,奇怪的是,我得到的是整个列表,而不是一个用户,所以问题是你得到的是一个列表:)而不是一个元素。我让你自己处理;)
{{ information?.username }}