I'm generating test/development dummy data with a node script in my Mongo database (using Mongoose) which includes Geolocation coordinates. (sets of lat/lon
). Schema follows:
location: {
type: {
type: String,
enum: ["Point"], // 'location.type' must be 'Point'
default: "Point",
},
coordinates: {
type: [Number],
required: true,
},
geocoded: {
type: String, // this has to be done with an external API
},
},
For that reason, I have an external (paid) Reverse Geocoding API which I want/need to call for each document/set of coordinates. The Geocoding API though has a rate limiter so I'm hitting 429
- too many requests. I'm looking for a clean and simple solution to run my requests sequentially and add a throttling/waiting time ( for a specified number of milliseconds ) after each HTTP request.
messageSchema.pre("insertMany", async function save(next, docs) {
docs.map(async (doc) => { // now I understand I should replace map with for ... of or for ... in
[err, response] = await to(
reverseGeocode(
doc.location.coordinates[0],
doc.location.coordinates[1]
)
);
if (err) {
next(err);
}
doc.location.geocoded = response;
});
});
The reverseGeocode signature:
reverseGeocode: (lon, lat) =>
axios({
baseURL: "https://eu1.locationiq.com/",
url: "v1/reverse.php",
params: {
lat,
lon,
key: geocodeKey,
},
}).then((response) => response.data),