Start a new topic

Business Logic Request module: no response from google maps geocode api

Hi,

I'm a newbie to Kinvey Business Logic, and I'm not getting any response from Google Maps API via the request module. I've checked all resources and tried everything I could find/think of. Can anyone tell me what I'm missing? I hope the comments included in the code below explain what it is I'm trying to accomplish, and what's working or not.



function onPreSave(request, response, modules){

var logger = modules.logger;

var longitude = -122.42889404296875; //hardcoded for testing; in production: request.body._geoloc[0];

var latitude = 37.69808343181304; //hardcoded for testing; in production: request.body._geoloc[1];

modules.collectionAccess.collection('Locations').geoNear(longitude, latitude, {'maxDistance':.01}, function (err, docs) {

if (err) {

logger.error('Query failed: '+ err);

} else {

if (docs.length!=0){ //this works when testing latlng near a saved Location

logger.info(docs[0].obj.name); //assumes the first found is right/closest

} else { //if a saved location isn't nearby, reverse geoCode the latlng to get an address

var uriString = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';

modules.request.request(uriString, function(error, response, body) {

logger.info("error: " + error); //none of these logger.info's are getting called

logger.info("response: " + response);

logger.info("body: " + body);

});

}

return response.continue(200);

}

});

}

Hi Dave, I tagged this under Business Logic so we can get the right eyes on it.
The problem is that you are calling response.continue before the request to Google Maps is complete. Since request.request is asynchronous, as soon as the call to request begins, code continues within the geoNear callback, where you call response.continue. This causes any future callbacks to be discarded.



Another note, response.continue does not take a parameter, so the 200 is ignored. Response status is only valid in response.complete().
Login or Signup to post a comment