I installed a pre-save function for my schema, which looks like this:
LocationSchema.pre('save', function(next) {
geocoder.geocode(this.address, function ( err, data ) {
if(data && data.status == 'OK'){
this.lat = data.results[0].geometry.location.lat;
this.lng = data.results[0].geometry.location.lng;
}
next();
});
});
So, I would like to geocode the location address and populate the lat and lng attributes before actually saving the model. The function published above performs geolocation, but this.lat and this.lng are not saved. What am I missing here?
novon source
share