Reputation: 145
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;
}
});
},
Upvotes: 1
Views: 142
Reputation: 1705
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
Upvotes: 2