I'm fetching data from an api to display to Leaflet map application. The problem is that some of the data has null values or even 0 values in the coordinates. This gives me an error (for null values) "Cannot read properties of null (reading 'coordinates'). How can I filter to avoid this data as they are not wanted when there is no valid coordinates?
This is the lines of code I am using and it works fine when all data is valid but now I got errors because of null values.
Did try to add a filter from this example to remove null values but still reading null for coordinates...
let geojson = {};
// API for places...
const api_url = "https://xxxxxx"
addPlaces()
.then(response => {
//console.log('API is Working!');
})
.catch(error => console.log(error.message));
async function addPlaces() {
const response = await fetch(api_url);
const data = await response.json();
geojson['type'] = 'FeatureCollection';
geojson ['features'] = [];
for (i = 0; i < data.length; i++) {
x = data[i].Address.coordinate.longitude;
y = data[i].Address.coordinate.latitude;
[x].filter(x => x);
[y].filter(x => x);
var newFeature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [x,y]
},
"properties": {
"x": data[i].Address.coordinate.longitude,
"y": data[i].Address.coordinate.latitude,
"validFrom": data[i].validFrom,
"name": data[i].name,
"placeId": data[i].placeId,
"typeId": data[i].typeId,
"streetName": data[i].Address.streetName,
"streetNumber": data[i].Address.streetNumber,
"streetLetter": data[i].Address.streetLetter,
"postalCode": data[i].Address.postalCode,
"city": data[i].Address.city,
"countryCode": data[i].Address.countryCode
}
}
geojson ['features'].push(newFeature);
}
console.log(JSON.stringify(geojson));
loadData (geojson);
};