So I want to put the lat en lng from the get request in an Array, and write that array to output.json. However, when I run this app now, I the order of the lat lon doesnt seem to match the order of the postal codes. How is that so?
var jsonarr = []
app.get('/scrape', function (req, res) {
for (var i = 0; i < dataset.length; i++) {
axios.get('https://maps.googleapis.com/maps/api/geocode/json?address=' +
dataset[i]._id + '+Netherlands&***')
.then(function (response) {
var contentlat = (response.data.results[0].geometry.location.lat);
var contentlon = (response.data.results[0].geometry.location.lng);
// console.log(lat, lon)
var json = { lat: "", lon: "" };
json.lat = contentlat;
json.lon = contentlon;
jsonarr.push(json)
console.log(jsonarr)
})
.catch(function (error) {
console.log(error);
});
}
fs.writeFile('output.json', JSON.stringify(jsonarr), function (err) {
// console.log('File successfully written! - Check your project directory for the output.json file');
})
The console does print the filled array. The dataset '_id' are postal codes from an external file