I want to pass json data that I get from an API into an array and display that information in my template and I don't know how to do that.
this error apears on my console :
my ts file :
this.api.getJoursFeries(year).subscribe(
(data: feries[]) => {
this.joursFeries = data;
// console.log(data);
}, (error: HttpErrorResponse) => {
console.log(error);
}
)
my service file :
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { feries } from '../models/feries';
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiBaseUrl = 'https://calendrier.api.gouv.fr/jours-feries/metropole'
constructor(private http: HttpClient) { }
getJoursFeries(annee : number): Observable<feries[]> {
return this.http.get<feries[]>(`${this.apiBaseUrl}/${annee}.json`);
}
}
The JSON response from https://calendrier.api.gouv.fr/jours-feries/metropole/2021.json is not an Array but a Javascript Object with different Key-Value pairs.
In your service you need to map the JSON object (from the server) to an Array (that you can use with *ngFor).
Let's assume the feries are defined as:
then in your service you can change the code for getting the data into:
Can you share the feries type? Then I can update the answer properly.