加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular 4无法读取未定义的属性“id”

发布时间:2020-12-17 17:55:00 所属栏目:安全 来源:网络整理
导读:我正在尝试使用angular 4来调用虚拟web api,这是我的组件 export class AppComponent { title = 'app'; url = 'https://api.ipify.org/?format=json'; results: any; constructor(private http: Http) {this.getData().subscribe(data = { this.results = da
我正在尝试使用angular 4来调用虚拟web api,这是我的组件

export class AppComponent {
    title = 'app';
    url = 'https://api.ipify.org/?format=json';
     results: any;

 constructor(private http: Http) {

this.getData().subscribe(data => {
  this.results = data.json();

  })
 }

 getData() {
return this.http.get(this.url);
 }

}

但当我尝试显示它时,我得到的结果,但也有一个错误的屏幕

ERROR TypeError: Cannot read property 'ip' of undefined

我想要理解的是为什么它被显示,因为在一瞬间之后数据被很好地显示.

解决方法

在您的* ngFor中,您可以将其更改为* ngFor =“let r of results | async”

您的问题是数据尚未存在,因此无法读取id属性.

如果使用异步管道,它应该在显示数据之前等待数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读