Start a new topic

CollectionAccess Query Stopped Working

Our findAndModify query worked perfectly a day or two ago, but now it never calls the callback. It does the upsert operation correctly, but then just appears to die.



I saw that there was a resent update, could this have broke our query? It could be that we implemented it wrong, and the fix could have broken our incorrect implementation?



Any help would be appreciated. Thanks :)



CODE:



var logger = modules.logger;

var collections = modules.collectionAccess;

var places = collections.collection('places');



...

//modules.async.map calls parseResult



var parseResult = function(result, callback) {

logger.info(result);

places.findAndModify({gp_id : result.id}, [], {

gp_id : result.id,

gp_ref : result.reference,

name : result.name

}, {new:true, upsert:true, w:1}, function(err, doc) {

logger.info('callback');

if (err) {

logger.error(err);

} else {

logger.info(doc);



var placeLat = result.geometry.location.lat;

var placeLng = result.geometry.location.lng;

var distance = getDistanceInMiles(lat, lng, placeLat, placeLng);



var place = {

name : result.name,

id : doc.entity._id,

reference : result.reference,

rating : result.rating,

location : {

lat: placeLat,

lng: placeLng

},

distance : distance,

iconUrl : result.reference

};



callback(null, place);

}

});

};

Not quite sure, although we are dealing with several layers of callbacks, so we need to try to untangle them.



What happens if you just try to execute the query in isolation, by itself, without the request and the async? Try running one query with a statically-defined 'result' object, in the main body of onRequest, commenting out the other code and putting response.complete in the query callback?
Agreed, but logger.error(err); is never being called. So, I am inclined to say that is not the case.



From our probing, it looks like the findAndModify call is the last thing that is called (in parallel). Do you see anything wrong with that call?



Thanks!
Is it possible that one of the array elements is causing an error in parseResult? The parseResult functions are called for each element of the array in parallel, and if an error occurs in any of them, the callback from async.map is immediately called with the error, which will call response.complete. This causes all further callbacks to be cancelled.
I wanted to avoid that, but here it is. Thanks for looking into it. :)



```var GOOGLE_PLACES_API_KEY = 'KEY';

function getDistanceInMiles(lat1,lon1,lat2,lon2) {

// Courtesy of http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points

var R = 3963.1676; // Radius of the earth in miles

var dLat = deg2rad(lat2-lat1); // deg2rad below

var dLon = deg2rad(lon2-lon1);

var a =

Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *

Math.sin(dLon/2) * Math.sin(dLon/2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c; // Distance in miles

return d;

}



function deg2rad(deg) {

return deg * (Math.PI/180)

}



/**

* onRequest hook. Fetches the image URL using Google Places.

*/

function onRequest(request, response, modules) {

// Extract modules.

var logger = modules.logger;

var req = modules.request;

var utils = modules.utils;

var async = modules.async;

var collections = modules.collectionAccess;



var places = collections.collection('places');



// Extract the keyword

var keyword = request.body.keyword;

var lat = request.body.latlng.lat;

var lng = request.body.latlng.lng;

var radius = request.body.radius;



var url = [

'https://maps.googleapis.com/maps/api/place/search/json?sensor=false',

'key=' + GOOGLE_PLACES_API_KEY,

'keyword=' + keyword,

'location=' + lat + ',' + lng,

'radius=' + radius

].join('&');



logger.info(url);



var parseResult = function(result, callback) {

logger.info(result);

places.findAndModify({gp_id : result.id}, [], {

gp_id : result.id,

gp_ref : result.reference,

name : result.name

}, {new:true, upsert:true, w:1}, function(err, doc) {

logger.info('callback');

if (err) {

logger.error(err);

} else {

logger.info(doc);



var placeLat = result.geometry.location.lat;

var placeLng = result.geometry.location.lng;

var distance = getDistanceInMiles(lat, lng, placeLat, placeLng);



var place = {

name : result.name,

id : doc.entity._id,

reference : result.reference,

rating : result.rating,

location : {

lat: placeLat,

lng: placeLng

},

distance : distance,

iconUrl : result.reference

};



callback(null, place);

}

});

};



// Lookup using Google Places.

req.get({

uri : url,

followRedirect: false

}, function(err, res) {

// Log any errors.

if (err) {

logger.error(err);

response.body.debug = err;

response.complete(500);

} else {

//logger.info(JSON.parse(res.body).results);

async.map(JSON.parse(res.body).results, parseResult, function(err, results) {

if (err) {

logger.error(err);

response.body.debug = err;

response.complete(500);

} else {

logger.info(results);

response.body = results;

response.complete();

}

});

}

});

}```
Can you provide the whole BL function, from beginning to end?



Thanks
There is no response.continue, and the complete is called in a callback from the parseResult function.



This is what calls the above function:



async.map(JSON.parse(res.body).results, parseResult, function(err, results) {

if (err) {

logger.error(err);

response.body.debug = err;

response.complete(500);

} else {

logger.info(results);

response.body = results;

response.complete();

}

});
Where is your response.continue or response.complete calls? If either is called before the callback is invoked, the callback is cancelled and execution stops.
Login or Signup to post a comment