Whenever I attempt to execute the method in my class (getCoordinates())
, an Error is thrown:
ERROR TypeError: this.locations[0].getCoordinates is not a function
What am I doing wrong?
The service method I'm using:
getLocations(){
return this.http.get<Location[]>(this.locationUrl);
}
This is how my model looks like:
export class Location {
id: number;
objectTypeId: number;
type: string;
fields: Fields;
fav: boolean;
typeless: boolean;
pages: number;
lastmodified: string;
public getCoordinates(): number {
return this.id;
}
}
export class Fields {
city: string;
street: string;
company: string;
plz: string;
info: string;
}
And here's the component code snippet:
public locations: Array<Location> = [];
ngOnInit() {
this.getAddresses();
}
getAddresses() {
this.enaioService.getLocations()
.subscribe(data =>
this.locations = data,
(error: HttpErrorResponse) => this.errorMessage = error.message
);
}
onClick() {
console.log(this.locations[0].getCoordinates()); // This is where the error occurs
}
https://stackblitz.com/edit/angular-bjgtn4
Project link: https://bitbucket.org/sven_hagemann/myproject/src/master/
Clone: git clone https://[email protected]/sven_hagemann/myproject.git