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>

ARR.s
  • 769
  • 4
  • 20
  • 39
  • 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. – JB Nizet Mar 07 '17 at 17:19
  • what should I do T_T – ARR.s Mar 07 '17 at 17:28
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – eko Mar 07 '17 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. – JB Nizet Mar 07 '17 at 17:30
  • 1
    @ARR.s, check echonax's answer: http://stackoverflow.com/a/42630520/6294072 and this: http://stackoverflow.com/a/42654240/6294072 – AT82 Mar 07 '17 at 17:41
  • If 'data' from subscribe is undefined then it might be a service problem. Post getImage code as well please – sTx Mar 07 '17 at 17:49
  • @sTx data is not undefined, I want to set `this.foundRepos[i].se` from data in subscribe – ARR.s Mar 07 '17 at 17:53
  • So this.fff is undefined? If so this might happen if this.fff is not declared as class variable or if data.json is undefined – sTx Mar 07 '17 at 17:56
  • @sTx yes is not undefined but out subscribe this.fff is undefined – ARR.s Mar 07 '17 at 17:58
  • I ve updated the comment above. Apart from this, have u tried to set foundrepos directly in subscribe? – sTx Mar 07 '17 at 18:04
  • @sTx Can you give example – ARR.s Mar 07 '17 at 18:14
  • this.foundRepos[i].se = data.json(); sorry I m on mobile – sTx Mar 07 '17 at 18:16
  • @ARR.s, check the answer I provided. Currently you are assigning the value outside the subscription, where fff is still undefined. You need to do it inside the curly brackets in subscription... – AT82 Mar 07 '17 at 18:20
  • And with your example: export class youclass{ fff:any}. Now you should be able to use this.fff in and outside of subscribe. – sTx Mar 07 '17 at 18:20
  • Oh, just came in. Because get is async call, the response is not ready by the time this.foundRepos[i].se = this.fff is executed. So this.fff is undefined – sTx Mar 07 '17 at 18:23
  • @sTx `this.foundRepos[i].se = data.json();` Cannot set property 'se' of undefined – ARR.s Mar 07 '17 at 18:26
  • Yeah, because 'i' has no value inside subscribe, or so I think. Use the console breackpoints and check vor values – sTx Mar 07 '17 at 18:34
  • @sTx Can you create discussion between you and me? – ARR.s Mar 07 '17 at 18:39
  • If only I would know how :) I m still on the phone – sTx Mar 07 '17 at 18:41
  • @ if you have example for me? – ARR.s Mar 07 '17 at 18:46
  • @JB Nizet this link , I try is still error – ARR.s Mar 07 '17 at 18:52

1 Answers1

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();
        });
   }
}
AT82
  • 71,416
  • 24
  • 140
  • 167
  • 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` – ARR.s Mar 08 '17 at 01:34
  • 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 :( – AT82 Mar 08 '17 at 08:11