I have created a GeoJSON list and am trying to export it to file via a button in the webpage. Everything works fine except that the file that is exported only displays 'undefined' and none of the GeoJSON data. What am I doing wrong?
HTML:
<button onclick="exportToJsonFile()">Download GeoJSON</button>
Script:
var api = $.getJSON("https://api.data.gov.sg/v1/environment/rainfall?date=2019-07-03",
function rainfall(data_rainfall){
apiGeo = { type: "FeatureCollection", features: []};
//---- apiGeo is populated here ----//
console.log(apiGeo); //displays data in GeoJSON format in browser console
});
function exportToJsonFile(apiGeo) {
let dataStr = JSON.stringify(apiGeo);
let dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(dataStr);
let exportFileDefaultName = 'data.json';
let linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', exportFileDefaultName);
linkElement.click();
}