1

Stuck with error = Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Service code =

`export class PokemonServiceService {
    constructor(private http: HttpClient) { }

    get getCards(): Observable<ICards[]>{
    return this.http.get<ICards[]>('https://api.pokemontcg.io/v1/cards?')
        }
    }
 export interface Ability {
  name: string;
  text: string;
  type: string;
   }
export interface Attack {
  cost: string[];
  name: string;
  text: string;
  damage: string;
  convertedEnergyCost: number;
}
export interface Weakness {
  type: string;
  value: string;
}
export interface Resistance {
  type: string;
  value: string;
}
export interface Card {
  id: string;
  name: string;
  nationalPokedexNumber: number;
  imageUrl: string;
  imageUrlHiRes: string;
  types: string[];
  supertype: string;
   subtype: string;
   ability: Ability;
   hp: string;
   retreatCost: string[];
   convertedRetreatCost: number;
   number: string;
  artist: string;
  rarity: string;
  series: string;
  set: string;
  setCode: string;
  text: string[];
  attacks: Attack[];
  weaknesses: Weakness[];
  resistances: Resistance[];
  evolvesFrom: string;
 }
export interface ICards {
  cards: Card[];
}

Component code =

`export class MainActivityComponent implements OnInit {
 PokemonData: ICards[];
 constructor(private service: PokemonServiceService ){}
 ngOnInit(): void {
 this.service.getCards.subscribe(data => this.PokemonData = data);      
            }



}`

Html code =

`<div class="card" *ngIf="PokemonData">
<div class="card-header">Pokemon</div>
<div class="card-block">
  <table class="table table-hover">
      <thead>
          <tr>
              <th>Naam</th>
              <th>id</th>
              <th>hp</th>
              <th>pokedexnumer</th>

          </tr>
      </thead>
      <tbody>
          <tr *ngFor="let d of PokemonData">      <!-- | async -->
              <td>{{d.name}}</td>
              <td>{{d.id}}</td>
              <td>{{d.hp}}</td>
              <td>{{d.nationalPokedexNumber}}</td>
          </tr>
      </tbody>
  </table>

)`

and if I use this in my component

ngOnInit(): void {
this.service.getCards()
            .subscribe(data => {
              console.log(data);
            })

then i get the data from the api (cards) , but if i try to sort them in a list i get this error = Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

1
  • The API doesn't actually return an array. It returns an object, which contains a property called cards, which is an array Commented May 19, 2018 at 17:31

2 Answers 2

1

The API doesn't return an array, it returns an object with a cards property.

Change your service, like so:

get getCards(): Observable<ICards>{
    return this.http.get<ICards>('https://api.pokemontcg.io/v1/cards?')
}

You component like so:

PokemonData: ICards;

And your template like so:

<tr *ngFor="let d of PokemonData.cards">
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help !! !!! changing <tr *ngFor="let d of PokemonData.cards"> fixed it :)
@Inias Glad to help, please feel free to accept the answer if this has fixed your problem :)
0

What is your the output on logging the data? Maybe it's wrapped like this, try to unwrap it first.

The response

[{someData},{someData}]

Unwrap

function readData < T > (xhrData: any) {

  try {


    var len = Object.keys(xhrData).length;

    var temp: Array < T > = [];

    for (var x = 0; x < len; x++) {
      //unwrap it and transfer it to an array -
      temp[temp.length] = xhrData[x]; //example only
    }
  } catch (e) {
    return [];
  }


  return temp;
}

I like to wrap my reader on a try catch block to also catch error responses from the server if its not a JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.