Encountering an Error Message...
ERROR TypeError: Cannot read properties of undefined (reading 'geoCoord')
at Object.next (customers.service.ts:16:38)
When assigning fixed values to "lon" and "lat" variables, like 51.1634 and 10.4477, the function works. There seems to be an issue with the get request or how I am accessing the JSON data. Any assistance would be appreciated.
The error is pinpointed to this line; why can't I access the values?
const lon = c.customerAdress.geoCoord.longitudeCoord;
const lat = c.customerAdress.geoCoord.latitudeCoord;
This is what my Service entails
export class CustomersService {
customers: string = '../../assets/data/customerdata.json';
constructor(private http: HttpClient) {}
makeCustomerMarkers(map: L.Map): void {
this.http.get(this.customers).subscribe((res: any) => {
for (const c of res.customerArray) {
const lon = c.customerAdress.geoCoord.longitudeCoord;
const lat = c.customerAdress.geoCoord.latitudeCoord;
const marker = L.marker([lat, lon]);
marker.addTo(map);
}
});
}
}
This is where the service is invoked in a component
ngAfterViewInit(): void {
this.initMap();
this.customersService.makeCustomerMarkers(this.map);
}
Below is the data structure being used
{
"customerArray": [
{
"customerAddress": {
"country": "Deutschland",
"geoCoord": {
"longitudeCoord": 10.4477,
"latitudeCoord": 51.1634
}
},
"phone": "0145221551",
"eMail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7b3a2b4b387b3b5a6b4afaaa6aeabe9a3a2">[email protected]</a>",
"homepage": "www.google.de",
},
{
"customerAddress": {
"country": "Deutschland",
"geoCoord": {
"longitudeCoord": 10.4477,
"latitudeCoord": 51.1634
}
},
"phone": "0145221551",
"eMail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aedacbdddaeedadccfddc6c3cfc7c280cacb">[email protected]</a>",
"homepage": "www.google.de",
},
{
"customerAddress": {
"country": "Deutschland",
"geoCoord": {
"longitudeCoord": 10.4477,
"latitudeCoord": 51.1634
}
},
"phone": "0145221551",
"eMail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b0f1e080f3b0f091a0813161a1217551f1e">[email protected]</a>",
"homepage": "www.google.de",
}
]
}