Villemh Villemh - 2 days ago 5
Javascript Question

How to return from remote API call in Meteor Method?

I have a call to API from meteor method. How can I return the data to the client? I have no success as seen below:

Meteor.methods({
'geCoordinates': function(distance,location) {
this.unblock();
geocoder.geocode(location, function ( err, data ) {
if (err) {
console.log("See on error " + err)
} else {
lat = data.results[0].geometry.location.lat
lng = data.results[0].geometry.location.lng
}
url = 'http://localhost:3005/events?lat='+lat+'&lng='+lng+'&distance='+distance+'&sort=venue&accessToken=1048427405248222|u4dBjiRw-9gdsgml1puWYFGrEvw'
})
return url
}
})

Answer

You can use Npm future to return data synchronously , Using Npm you code will be look something like this

On server side

var future = require('future');

Meteor.methods({
'geCoordinates': function(distance,location) {
    this.unblock();
    var fut = new Future();
    geocoder.geocode(location, function ( err, data ) {
        if (err) {
            console.log("See on error " + err)
         } else {
            lat = data.results[0].geometry.location.lat 
            lng = data.results[0].geometry.location.lng
        }
            url = 'http://localhost:3005/events?lat='+lat+'&lng='+lng+'&distance='+distance+'&sort=venue&accessToken=1048427405248222|u4dBjiRw-9gdsgml1puWYFGrEvw'
     fut.return(url); // returns the url
    })
return fut.wait(); // waits until the url is ready
}

})

Note: This will return data synchronously.

Comments