ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

I'm using a geolocation server script from an example you guys developed several years ago and it always seems to find the coordinates, but when it searches the database for names at the location I get inconsistent error messages "An error occured". (Please see attached server code). What I mean is sometimes it works and sometimes it doesn't. Help please - the server code is below. As I said it is from a past tutorial of yours.

Server Code

// Database Id
var dbId = "5463d26be4b03a87ba7abf34"; //Change this value to your database id

var responseBody = {};

// Get request parameters
var latitude = request.object().latitude;
var longitude = request.object().longitude;
var radius = request.object().radius;

// The function that calculates the distance between two points

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

var R = 6378137; // Earth’s mean radius in meters

var dLat = (lat2 - lat1)*Math.PI/180;

var dLon = (lon2 - lon1)*Math.PI/180;

var lat1 = lat1*Math.PI/180;

var lat2 = lat2*Math.PI/180;

var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);

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

var d = R * c;

return d;

}

try {

// Check if all the necessary parameters are present and valid

if (latitude && longitude && radius && !isNaN(latitude) && !isNaN(longitude) && !isNaN(radius)) {

var results = [];

var params = {};

// Sort the results by contact name

params.sort = "contactName";

// Get all customers from the database

var customers = Collection.query(dbId, "customers", params);

for (var i = 0; i < customers.length; i++) {

// Geocode customer's address with Google API

var XHRResponse = XHR.send("GET", "https://maps.googleapis.com/maps/api/...", {

"parameters": {

"address": customers.address,

"sensor": "false"

}

});

console.log(XHRResponse);

if (XHRResponse.status == 200) {

// Get coordinates from the response

var lat = JSON.parse(XHRResponse.body).results[0].geometry.location.lat;

var lng = JSON.parse(XHRResponse.body).results[0].geometry.location.lng;

// If the customer's coordinates are within the radius,

// add the customer record to the results array

if (getDistance(lat, latitude, lng, longitude) , 400);

}

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server script location/database search issues

Steven,

Recently - (Not sure by the way this is your problem - however - it has caused a number of our code snippets not to work) - Google is enforcing limits on 'batch processes'....

Some items in a batch will fail - because you are exceeding the maximum allowed number of calls - to the API - without licensing your API key - for batch calls.

you can change your code above - to see if this is the issue by adding an else clause after your bracket - and then examining the trace.

if (XHRResponse.status == 200) {

your code is already here.....

} else {

console.log( 'Error Acquiring Coordinates: ', JSON.stringify( XHRResponse ) )

}

Also - notably - in your code above - you show the try { portion of the code - however - you have not exposed the } catch (e) {} part of your code - which would likely show the part of the code producing the "An error occurred"

Not sure if this helps.

Best,

Bruce

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Server script location/database search issues

Hello,

Could you please clarify what exactly does not work? Do you see any error in the Server Code trace tab?

ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

Haven't had time to check yet. I'll let you know.

ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

I added your code recommendation Bruce but that had no affect. After multiple runs, again with intermittent success, I am beginning to think more about your batch calls limits theory.

Serhii - the error in the server code trace is attached below.

Image

ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

Bruce,

How do I license my API Key for Batch Calls? If you don't mind providing that help.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server script location/database search issues

Steven,

Late here now... I’ll look in the morning for yuu.

If you’d like to chat by Skype tomorrow ... to close out on this ... email me your Skype address or we can arrange via email .

Best,

Bruce

ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

Sorry Bruce - I'm doing some teaching. Is sometime after 5pm EST good for you?

ssquire
Posts: 0
Joined: Tue Feb 12, 2013 4:36 am

Server script location/database search issues

Bruce,

My Skype email is: a href="mailto:steve@ttellc.com" rel="nofollow"steve@ttellc.com/a

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Server script location/database search issues

Got your note - see email from me.... pls respond with Timezone and a couple of free times when we can chat.... would like to help you past your blocker!

Return to “Issues”