Home > front end >  Get Object from http get API result
Get Object from http get API result

Time:02-16

I know question has already asked but I can't do it with my get result.

My service get data from API return this kind of result :


{
    "nhits": 581,
    "parameters": {
        "dataset": "communes-belges-2019",
        "rows": 1,
        "start": 0,
        "facet": [
            "niscode",
            "region",
            "nis_code_region"
        ],
        "format": "json",
        "timezone": "UTC"
    },
    "records": [
        {
            "datasetid": "communes-belges-2019",
            "recordid": "65d40b7bc42f766b4fdb04c4a985766dc8b51717",
            "fields": {
                "shape_area": 79397718.576,
                "mun_name_upper_fr": "ÉTALLE",
                "arr_name_fr": "Virton",
                "region": "Région wallonne",
                "niscode": "85009",
                "mun_off_language": "FR",
                "geo_shape": {
                    "coordinates": [
                        [
                            [
                                5.678490965,
                                49.687217222
                            ],
                            [
                                5.678462422,
                                49.6873304
                            ]
                        ]
                    ],
                    "type": "Polygon"
                },
                "prov_name_fr": "Luxembourg",
                "namefre": "Étalle",
                "nom_commune": "Étalle",
                "nis_code_region": "03000",
                "mun_name_fr": "Étalle",
                "reg_name_fr": "Région wallonne",
                "mun_area_code": "BEL",
                "modifdate": "2007-01-05",
                "mun_type": "Commune/Gemeente/Gemeinde",
                "mun_name_lower_fr": "étalle",
                "prov_code": "80000",
                "year": "2021-01-01",
                "geo_point_2d": [
                    49.6639160352,
                    5.60896600843
                ]
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    5.60896600843,
                    49.6639160352
                ]
            },
            "record_timestamp": "2019-05-24T09:44:14.333000 00:00"
        }
    ],
    "facet_groups": [
        {
            "name": "niscode",
            "facets": [
                {
                    "name": "11001",
                    "count": 1,
                    "state": "displayed",
                    "path": "11001"
                },
                {
                    "name": "33021",
                    "count": 1,
                    "state": "displayed",
                    "path": "33021"
                },
                {
                    "name": "33029",
                    "count": 1,
                    "state": "displayed",
                    "path": "33029"
                },
                {
                    "name": "33037",
                    "count": 1,
                    "state": "displayed",
                    "path": "33037"
                }
            ]
        },
        {
            "name": "region",
            "facets": [
                {
                    "name": "Région flamande",
                    "count": 299,
                    "state": "displayed",
                    "path": "Région flamande"
                },
                {
                    "name": "Région wallonne",
                    "count": 262,
                    "state": "displayed",
                    "path": "Région wallonne"
                },
                {
                    "name": "Région de Bruxelles-Capitale",
                    "count": 19,
                    "state": "displayed",
                    "path": "Région de Bruxelles-Capitale"
                }
            ]
        },
        {
            "name": "nis_code_region",
            "facets": [
                {
                    "name": "02000",
                    "count": 299,
                    "state": "displayed",
                    "path": "02000"
                },
                {
                    "name": "03000",
                    "count": 262,
                    "state": "displayed",
                    "path": "03000"
                },
                {
                    "name": "01000",
                    "count": 19,
                    "state": "displayed",
                    "path": "01000"
                }
            ]
        }
    ]
}

For the moment my service get data like this :


  getProvinces(): Observable<any[]>{
    return this._http.get<any>(this.apiProvinces)
      .pipe(
         map((response: any) => response.records as any[]),
         catchError(this.handleError)
         )         
  }

It returns a Observable<any[]> but I would like to get an object.

Therefore I defined a class with the below properties.


export class Record{
    region      : string;
    nom_commune : string;
    prov_name_fr: string;
    records?: [];

    constructor(reg: string, commune: string, prov: string) {
        this.region = reg;
        this.nom_commune = commune;
        this.prov_name_fr = prov;
    }
}

To reach that I try by replacing any[] by Record[] like below but it doesn't work.


  getProvinces(): Observable<Record[]>{
    return this._http.get<Record[]>(this.apiProvinces)
      .pipe(
         map(response => response as Record[]),
         catchError(this.handleError)
         )         
  }

In my component I defined an Observable<Record[]> like this :


public results$!: Observable<Record[]>;

And call the service like this :


this.results$ = this.apiService.getProvinces();

And try to see content in the html part:

<div *ngFor="let p of (results$ | async)">
     {{p | json}}
</div>

I have the following error message : "Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."

And can't access to my object.

Any suggestions is helpfull because. I'm absolutely new to ANgular.

Thanks

CodePudding user response:

When you create an observable, you don't actually make a request to your api. To make the request and get the data from the observable, you need to subscribe.

this.apiService.getProvinces().subscribe((res) => (this.results = res));

This will execute the get request and return your data to the callback function. Get requests are finite observables, so you don't need to unsubscribe, but you will need to subscribe again if you want to make another request.

  • Related