I think my Problem is that the API delivers an Object instead of an array. So i need to Modify the Object to be an Array?

Viewed 36

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

I am trying to fetch data from API but it's showing me this error, as I am still learning angular please guide me on how to tackle this kind of errors.

  1. metadata-service.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Pincodes } from 'src/app/Model/Pincodes';
import { baseURL } from 'src/environments/environment';

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

  constructor(private http:HttpClient) { }

  public GetPincodes() : Observable<any> {
    console.log("Calling GetPincodes API")
    return this.http.get("/Metadata/api/metadata/getpincodes") // My API Link
  }

}

  1. API consist of the following data
{
"program": null,
"version": null,
"dateTime": null,
"success": true,
"errorCode": null,
"errorMessage": null,
"message": "Pincodes read Successfully",
"data": [
{
"pincode": 400708,
"village": "Airoli",
"taluka": "Thane",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 416007,
"village": "Kaamba",
"taluka": "Kalyan",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421102,
"village": "Atali",
"taluka": "Kalyan",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421501,
"village": "Ambarnath",
"taluka": "Ambarnath",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421503,
"village": "Ambeshiv",
"taluka": "Ambarnath",
"district": "Thane",
"state": "Maharashtra"
}
]
}

I am interested in the data array

  1. Pincode.ts

export class Pincodes {
    pincode : number
    village : string
    taluka : string
    district : string
    state : string
}
  1. register-page.component.ts
import { Component, OnInit } from '@angular/core';
import { data } from 'jquery';
import { Pincodes } from 'src/app/Model/Pincodes';
import { MetadataServiceService } from 'src/app/shared/services/metadata-service.service';
import { Albums } from "src/app/Model/albums";
import { Observable, of } from 'rxjs';
import {MatTableDataSource, MatTableModule} from '@angular/material/table';
import { report } from 'process';


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

export class RegisterPageComponent implements OnInit {


 observeData : Pincodes[]  
  modifiedText: any;
  PinSelected : any = {}

 
 


  constructor(private _metadataService: MetadataServiceService) {  }

  ngOnInit(){
    
   this._metadataService.GetPincodes()
   .subscribe(data => {
      this.observeData = data;
    });
  }
onPincodeSelected(val : Pincodes){
    console.log("value" + val);
    this.customFunction(val)
  }
  customFunction(val : Pincodes){
    this.modifiedText = "The Selected Pin was " + val.pincode + " and selected District is " + val.village
      console.log("modifiedText" + this.modifiedText);
      console.log("Pincode Selected" + this.PinSelected);
      console.log("observeData Selected" + this.observeData);
      
      
  }
  
}

  1. register-page.component.html
<select
        [(ngModel)]="PinSelected"
        (ngModelChange)="onPincodeSelected($event)"
      >
        <option *ngFor="let pin of observeData" [ngValue]="pin">
          {{ pin.pincode }}
        </option>
      </select>
      <div>
        <p>{{ modifiedText }}</p>
      </div>

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

Its showing this error.

1 Answers

The array you need to iterate is inside your api response object so in

this._metadataService.GetPincodes()
   .subscribe(data => {
      this.observeData = data;
    });

change this.observeData = data by this.observeData = data.data

Related