1

I want to get data json to show listpage but http.get is undefined

I want to set this.fff to this.foundRepos[i].se thanks for your help. :)

  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>

21
  • 1
    1. Indent your code properly to make it readable. 2. Post the exact and complete error message you get: the title says "http.get is undefined". The question says: "<---this undefined". Make things clear. Commented Mar 7, 2017 at 17:19
  • what should I do T_T Commented Mar 7, 2017 at 17:28
  • 2
    Possible duplicate of How do I return the response from an asynchronous call? Commented Mar 7, 2017 at 17:29
  • 1
    1. Indent your code properly to make it readable. 2. Post the exact and complete error message you get: the title says "http.get is undefined". The question says: "<---this undefined". Make things clear. Commented Mar 7, 2017 at 17:30
  • 1
    @ARR.s, check echonax's answer: stackoverflow.com/a/42630520/6294072 and this: stackoverflow.com/a/42654240/6294072 Commented Mar 7, 2017 at 17:41

1 Answer 1

2

When I tested it with the for loop, I came to the conclusion that i always is the length of the array inside the subscription, no matter where in the iteration we are. I'm not entirely sure why, so someone smarter, please enlighten me! :)

What seemed to work well was to use forEach or change the looping to something like this:

for(let x of this.foundRepos)

So please try the following, where x obviously is the whole current object from the array:

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();
        });
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much ,i got it! but I have a new error sometimes I get error 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
I'm not entirely sure about that error, seems like there is a problem on the api side. I draw this conclusion because of the 403 Forbidden error, since that is a server error, not client side. Sorry, but can't really help you with that issue :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.