所以我想循环来自JSON文件的邮政编码,以便将它们地理定位到lat& lon。但是,get()函数之后的代码似乎总是选择循环中的最后一个i元素。 console.log(dataset [i] ._ id)产生fith元素的postcal代码的五倍。那怎么样?
var jsonarr = []
app.get('/scrape', function(req, res){
for(var i = 0; i < 5; i++) {
var aantallen = dataset[i].count
axios.get('https://maps.googleapis.com/maps/api/geocode/json?address=' +
dataset[i]._id + '+Netherlands&key=*')
.then(function (response) {
// console.log(i)
var contentlat = (response.data.results[0].geometry.location.lat);
var contentlon = (response.data.results[0].geometry.location.lng);
var json = {lat: "", lon: "", aantal: ""};
json.lat = contentlat;
json.lon = contentlon;
json.aantal= aantallen;
console.log(dataset[i]._id)
jsonarr.push(json)
fs.writeFile('output.json', JSON.stringify(jsonarr, null, 4), function(err){
// console.log('File successfully written! - Check your project directory for the output.json file');
})
})
.catch(function (error) {
console.log(error);
});
}
res.send('Check your console!')
});