I have the following service:
import { Component } from '@angular/core';
import { ApiService } from './shared/api.service';
import {PowerPlant} from './shared/models/powerplant.model';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
powerPlants: PowerPlant[];
constructor(private apiService: ApiService) {
}
allPowerPlants(onlyActive: boolean = false, page: number = 1): void {
const path = `$/powerPlants?onlyActive${onlyActive}&page${page}`;
this.apiService.get(path).map() // TODO: parse and set the JSON to my model
}
}
In the get method of the apiService, this is what I do:
get(path: string, params: URLSearchParams = new URLSearchParams()): Observable<any> {
return this.http.get(`${environment.api_url}${path}`, { headers: this.setHeaders(), search: params })
.catch(this.formatErrors)
.map((res: Response) => res.json());
}
So I would like to parse this Json array, if there are any errors in any one of the array element, I would like to ignore it and populate the powerPlant array for the remaining valid ones! Any pointers?
EDIT: I tried out the suggestion as mentioned by the post below and I do get an error as shown in the screenshot:
Why is this? Is is complaining that PowerPlant is an interface and I need to provide values for the properties when I create a new instance of it?

mapmethod ofthis.apiService.get(path).map(), and assign the value passed on to the callback function topowerPlants; you can also useconsole.loginside the callback function to check whether everything is in order or not.