# Unable to display API data on frontend

**URL:** <https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338>\
**Category:** ionic-v3\
**Created:** [December 4, 2018, 1:55am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338 "2018-12-04T01:55:23Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaywrye](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jaywrye](https://forum.ionicframework.com/u/jaywrye)\
**Post date:** [December 4, 2018, 1:55am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/1 "2018-12-04T01:55:23Z")

</div>

Hi, I am a beginner in both Ionic and APIs. Currently, I have an API where I need to get data and display it on my app. I tried to display the data using the \*ngFor but it doesn’t work. Will anyone please guide me on this?

…/providers/christmas

```auto
  getDedi() {
   return this.http.get<DediResult>(`${this.dataApiUrl}?//some api`,
      { headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN'))});
  }

```

admin.ts

```auto
@Component({
  selector: 'page-admin',
  templateUrl: 'admin.html'
})
export class AdminPage {

  dedi: Dedi[];

  constructor(public navCtrl: NavController, public navParams: NavParams, public christmasProvider: ChristmasProvider, private storage: Storage) {
    console.log('constructor Testpage');
  }

  gotoDetailspage(dedi){
    this.navCtrl.push(DetailPage, {dedi: dedi});
  }

 ionViewDidLoad(){
    console.log('ionViewDidLoad Adminpage');

  
    this.christmasProvider.getDedi().pipe(
      map((dediResult: DediResult) => dediResult && dediResult.result.records)
    ).subscribe(dedid => {
      this.dedi = dedid;

      for(let i=0; i<this.dedi.length; i++)
      {
      console.log(this.dedi[i].song);
      console.log(this.dedi[i].pledge_amount);
      }
  });

```

admin.html

```auto
<ion-header>
    <ion-navbar color="primary">
      <button ion-button menuToggle>
        <ion-icon name="menu" style="color: #fff"></ion-icon>
      </button>
      <ion-title>
        <p class="header">ADMIN</p>
      </ion-title>
    </ion-navbar>
  </ion-header>

  <br><br><br>
  
  <ion-content padding>
      <h6>DEDICATION:</h6>
        <ion-item *ngFor ="let d of dedi" (click) = gotoDetailspage()>
        <ion-row>
          <ion-col align-self-start style="text-align: left;">
              <p>{{ d.song }}</p> <!--database value-->
              <p>{{ d.pledge_amount }}</p> <!--database value-->
          </ion-col>
        </ion-row>
      </ion-item>
    </ion-content>

```

…/models/songdedi

```auto
export interface DediData {
    data: DediResult;
}

export interface DediResult {
    result: DediRecords;
}

export interface DediRecords {
    records: Dedi[];
}

export interface Dedi {
    dedication_id: Int32Array;
    pledge_amount: Int32Array;
    phone_no: string;
    sing_to: string;
    sing_to_department: string;
    dedication_date: DateTime;
    dedication_time: Time;
    song: string;
    other_song_request: string;
    special_request: string;
    status: string;
    location: string;
    message: string;
    remain_anon: boolean;
}

```

Thanks guys

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 7:19am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/2 "2018-12-04T07:19:10Z")

</div>

> [@jaywrye](#):
>
> \<ion-item \*ngFor ="let d of **dedid**" (click) = gotoDetailspage()\>

your array name is wrong it should dedi instead of dedid

---

<div class="post-metadata">

**Author:** ![jqy](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jqy](https://forum.ionicframework.com/u/jqy)\
**Post date:** [December 4, 2018, 7:29am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/3 "2018-12-04T07:29:03Z")

</div>

Hmm, i tried that but it does not work as well…

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 7:32am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/4 "2018-12-04T07:32:20Z")

</div>

on ionViewDidLoad do one loop and console log the d.song and d.pledge\_amount same as you did in html

---

<div class="post-metadata">

**Author:** ![jaywrye](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jaywrye](https://forum.ionicframework.com/u/jaywrye)\
**Post date:** [December 4, 2018, 7:38am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/5 "2018-12-04T07:38:17Z")

</div>

I’m sorry i’m quite new to this. Sorry to trouble you but how do i perform a while loop for this case?

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 7:50am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/6 "2018-12-04T07:50:48Z")

</div>

put this in your ionViewDidLoad()

for(let i=0; i\<dedi.length; i++)  
{  
console.log(“song\*\*”, dedi[i].song);  
console.log(“pledg\*\*”, dedi[i].pledge\_amount);  
}

let me know what are you getting in console

---

<div class="post-metadata">

**Author:** ![jaywrye](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jaywrye](https://forum.ionicframework.com/u/jaywrye)\
**Post date:** [December 4, 2018, 7:57am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/7 "2018-12-04T07:57:22Z")

</div>

I am getting `ERROR TypeError: Cannot read property 'length' of undefined` and `ERROR TypeError: Cannot read property 'records' of undefined`. Console log is not displaying anything

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 8:01am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/8 "2018-12-04T08:01:36Z")

</div>

This is because that dedi array is not loaded as subscribing is take place in same function. will you wrap my above loop code put inside of your subscribe or use setTimeOut to delay my loop execution.

Also make sure that you have changed this dedid to dedi

> [@jaywrye](#):
>
> \<ion-item \*ngFor ="let d of **dedid**" (click) = gotoDetailspage()\>

I am in office otherwise i would able to resolve your problem via teamviewer or anyDesk

---

<div class="post-metadata">

**Author:** ![jaywrye](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jaywrye](https://forum.ionicframework.com/u/jaywrye)\
**Post date:** [December 4, 2018, 8:12am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/9 "2018-12-04T08:12:30Z")

</div>

I see, I have changed it already. Actually, I have modified my codes. I have made the amendments to the post and included your codes into the amendment as well. Please take a look and tell me if I am doing anything wrongly. Currently, it is throwing this error only `ERROR TypeError: Cannot read property 'records' of undefined`. Thanks a lot for your help btw!

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 8:19am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/10 "2018-12-04T08:19:12Z")

</div>

Hey you did not return the response.json() from the http call  
If you are failed to resolve this issue before 9PM IST i will try to do via teamviewer or AnyDesk.

---

<div class="post-metadata">

**Author:** ![jaywrye](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jaywrye](https://forum.ionicframework.com/u/jaywrye)\
**Post date:** [December 4, 2018, 8:27am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/11 "2018-12-04T08:27:34Z")

</div>

Hm, you mean i have to change my provider to

```auto
  getDedi() {
   return this.http.get<DediResult>(`${this.dataApiUrl}?//some api`,
      { headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN'))})
      .pipe(
        map((dediResult: DediResult) => dediResult && dediResult.result.records)
      );
  }

```

and my admin.ts to

```auto
ionViewDidLoad(){
    console.log('ionViewDidLoad Adminpage');

  
    this.christmasProvider.getDedi().subscribe(dedid => {
      this.dedi = dedid;

      for(let i=0; i<this.dedi.length; i++)
      {
      console.log(this.dedi[i].song);
      console.log(this.dedi[i].pledge_amount);
      }
  });

```

Did I understand it correctly? However, the error i get is still the same…

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 4, 2018, 9:28am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/12 "2018-12-04T09:28:54Z")

</div>

> [@jaywrye](#):
>
> getDedi() { return this.http.get\<DediResult\>(`${this.dataApiUrl}?//some api`, { headers: new HttpHeaders().set(‘X-XSRF-TOKEN’, this.getCookie(‘XSRF-TOKEN’))}) .pipe( map((dediResult: DediResult) =\> dediResult && dediResult.result.records) ); }

change this to

```auto
getDedi() {
   return this.http.get<DediResult>(`${this.dataApiUrl}?//some api`,
      { headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN'))})
      .pipe(
        map((response:Response) => {
 return response.json();
} 
)
      );
  }

```

Make sure that you import Response from angular/http

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [December 4, 2018, 9:43am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/13 "2018-12-04T09:43:10Z")

</div>

> [@indraraj26](#):
>
> change this to

No, don’t. This post makes zero sense. You are mixing old deprecated `Http` with more modern `HttpClient` syntax. `Http` does not have templated `get`, and `HttpClient` wants neither `Response` nonsense or calling `json()`.

---

<div class="post-metadata">

**Author:** ![jqy](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jqy](https://forum.ionicframework.com/u/jqy)\
**Post date:** [December 4, 2018, 1:25pm UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/14 "2018-12-04T13:25:43Z")

</div>

hi all, thanks for your help. I have managed to solve this issue.

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 5, 2018, 5:57am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/15 "2018-12-05T05:57:38Z")

</div>

> [@rapropos](#):
>
> No, don’t. This post makes zero sense. You are mixing old deprecated `Http` with more modern `HttpClient` syntax. `Http` does not have templated `get` , and `HttpClient` wants neither `Response` nonsense or calling `json()` .

deprecated http but still it is secure, i have been using all the time no issue till now.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [December 5, 2018, 6:01am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/16 "2018-12-05T06:01:38Z")

</div>

> [@indraraj26](#):
>
> i have been using all the time no issue till now.

Not with the syntax you posted, you haven’t.

If you import `http` from `@angular/http` and try to call `get` with a type argument as you suggest (`this.http.get<DediResult>`), TypeScript will puke.

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [December 5, 2018, 6:11am UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/17 "2018-12-05T06:11:21Z")

</div>

i have passed so many arguments with http.get sometime i pass id to get the user post. actually i didnt get the what kind of data will have dediresult Anyway he is already resolved the issue.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:07pm UTC](https://forum.ionicframework.com/t/unable-to-display-api-data-on-frontend/149338/18 "2021-07-19T21:07:59Z")

</div>

Closed on July 19, 2021, 9:07pm UTC


