【问题标题】:get data is undefined ionic2获取数据未定义
【发布时间】:2017-03-08 05:43:33
【问题描述】:

我想获取数据 json 以显示列表页,但 http.get 未定义

我想将this.fff 设置为this.foundRepos[i].se 谢谢你的帮助。 :)

  for(var i=0;i<this.foundRepos.length;i++){
            if(!this.foundRepos[i].search.isbn){
              this.foundRepos[i].se = "assets/read3.png";
            }
            if(this.foundRepos[i].search.isbn){
              this.foundRepos[i].se = this.foundRepos[i].search.isbn;
this.api.getImage(this.foundRepos[i].se).subscribe(
            data => {

              this.fff = data.json();
            },
            err => console.error(err),
            () => console.log('completed')
          );
            this.foundRepos[i].se = this.fff; <---this undefined
        }
              }

listpage.html

<ion-item text-wrap *ngFor="let item of  foundRepos" (click)="goToDetails(item)">
  <p>{{ item.type }}</p>
  <ion-thumbnail item-left>
    <img src="{{item.se}}">
</ion-thumbnail>

【问题讨论】:

标签: angular typescript ionic-framework ionic2


【解决方案1】:

当我使用 for 循环对其进行测试时,我得出的结论是,i 始终是订阅中数组的长度,无论我们在迭代中的哪个位置。我不完全确定为什么,所以请聪明的人赐教! :)

似乎效果不错的是使用forEach 或将循环更改为以下内容:

for(let x of this.foundRepos)

所以请尝试以下操作,其中x 显然是数组中的整个当前对象:

for(let x of this.foundRepos) {
   if(!x.search.isbn) {
      x.se = 'assets/read3.png';
   }
   if(x.search.isbn) {
      x.se = x.search.isbn;
      this.api.getImage(x.se)
        .subscribe( data => {
           x.se = data.json();
        });
   }
}

【讨论】:

  • 非常感谢,我明白了!但我有一个新错误,有时我会收到错误file_get_contents(https://www.googleapis.com/books/v1/volumes?q=isbn:0030723787): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
  • 我不完全确定那个错误,好像 api 方面有问题。我得出这个结论是因为 403 Forbidden 错误,因为那是服务器错误,而不是客户端错误。对不起,但不能真正帮助你解决这个问题:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
相关资源
最近更新 更多