I am using Meteor, whenever I make an api call to googles geocode and try to return values from it I get an undefined, I am using the callback to the api so there is definitely data there so I'm not sure as to what is causing it
callWeather = e => {
e.preventDefault();
console.log(this.state.address);
Meteor.call("geCoordinates", this.state.address, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
};
geCoordinates(address) {
googleMapsClient.geocode({ address }, (error, data) => {
if (error) {
console.log(error);
} else {
console.log(data.json.results[0].geometry.location.lat);
return data.json.results[0].geometry.location.lat;
}
});
},
This is a common mistake that most people do when they are starting. The problem here is that before your method code execute the callback function , data response is present in the client. There are many solution ot this:
But, I would recommend you to use Meteor.wrapAsync
like below:
let getGeoCode = Meteor.wrapAsync(googleMapsClient.geocode, googleMapsClient.geocode),
data = getGeoCode({ address }); // data contains data of your callback
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.