1

Am done importing the HttpClientModule, as well as registering it in the app module. However when i make the request from the API, i get an error in the console:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Below is my app.service.ts

import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http'
import {mydata} from './app'
import { Observable } from 'rxjs';


@Injectable({
  providedIn: 'root'
})
export class AppService {


  private url:string="https://allsportsapi.com/api/football/?&met=Videos&eventId=76387&APIkey=e09665167a2ccf5f2c7d15527c78d5d209eff826dea5b4dd0bdbc65b258be8de";


  constructor(private http : HttpClient) { }

  getData():Observable<mydata[]>
  {
    return this.http.get<mydata[]>(this.url);
  }


}
**Below is the code for the app.component.ts**
import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})





export class AppComponent  implements OnInit{
  title = 'movie';
  info:any[];
  constructor(private service:AppService){}
  ngOnInit()
  {
      this.service.getData().subscribe(data=>{this.info = data})
  }
}
**Below is the app.component.html**

<h1>Welcome To {{title}} stream</h1>
<table>
  <td *ngFor="let video of info ">

    <tr>{{video.result[0] | json}}</tr>
    <tr>{{video.result[1] | json}}</tr>
  </td>
</table>

**This is my interface**
export interface mydata
{
  success:number,
  result:string[]
}
**This is what the api returns**
{
  "success": 1,
  "result": [
    {
      "video_title": "Goal - 1:0 - 12'\n Poli A., Bologna",
      "video_url": "https://cc.sporttube.com/embed/leMCCCG/no-preload"
    },
    {
      "video_title": "Goal - 1:1 - 38'\n Babacar K., Sassuolo",
      "video_url": "https://cc.sporttube.com/embed/TeMCCCG/no-preload"
    },
    {
      "video_title": "Yellow Card - 86'\n Goldaniga E., Sassuolo",
      "video_url": "https://cc.sporttube.com/embed/EeMCCCG/no-preload"
    },
    {
      "video_title": "Goal - 2:1 - 88'\n Pulgar E., Bologna",
      "video_url": "https://cc.sporttube.com/embed/FeMCCCG/no-preload"
    },
    {
      "video_title": "Highlights",
      "video_url": "https://streamable.com/e/0d35p"
    }
  ]
}

4 Answers 4

3

Looks like your api returns an object, but you're expecting an array. Try to change you request a little bit:

  getData(): Observable<MyData[]> {
    return this.http.get<{ result: MyData[] }>(this.url).pipe(
      map(response => response.result)
    );
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I will recommend that you create a .TS interface matching your API JSON response.

Then update your method this.service.getData() so that it receives an array of your previously created .TS interface.

Finally, update info:any[]; to info:YourInterface[];

Comments

0

Try to use it like this

<table>
  <td *ngFor="let video of info.result ">

    <tr>{{video.video_title}}</tr>
    <tr>{{video.video_url}}</tr>
  </td>
</table>

Or

store data.result into this.info

 ngOnInit()
  {
      this.service.getData().subscribe(data=>{this.info = data.result})
  }

and

<tr>{{video.video_title | json}}</tr>
<tr>{{video.video_url | json}}</tr>

Comments

0

If your API returns object insted of array and you want to loop through an object you can use keyvalue pipe.

Transforms Object or Map into an array of key value pairs.

@Component({
  template: `<div *ngFor="let item of data | keyvalue">
      {{item.key}} - {{item.value}}
    </div>`
})
export class MyComponent {
  data = { "key": "value", "key2": "value2" };
}

2 Comments

I didnt get you so well..Please elaborate more about how i can transform the data
@WaiswaDonnie Here you can see an example stackblitz.com/edit/angular6-keyvaluepipe-demo-762uea

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.