Page 1 of 1

Creating server script

Posted: Wed Aug 06, 2014 5:39 pm
by UH

Hi,

Was hoping if anyone could help me with some direction.

I am following this tutorial: http://devcenter.appery.io/tutorials/...

I have created a database, UI, and Geolocation service

My question is, if I am using coordinates in the database instead of addresses, how would I modify the server script as it uses Google Maps geocoding API to convert addresses into coordinates (in the tutorial).

Thanks!


Creating server script

Posted: Wed Aug 06, 2014 7:12 pm
by Evgene Karachevtsev

Hello,

In this case, you do not need to perform the request of coordinates at:
prevar XHRResponse = XHR.send("GET", "https://maps.googleapis.com/maps/api/geocode/json", {
"parameters": {
address: customers.address,
"sensor": "false"
}
});/pre
You can immediately get these coordinates, and use them in your code


Creating server script

Posted: Wed Aug 06, 2014 9:27 pm
by UH

Thanks. That's what I was thinking. So just take out that part and keep the rest as it is?


Creating server script

Posted: Wed Aug 06, 2014 9:51 pm
by obullei

Hello!

You are right.


Creating server script

Posted: Thu Aug 07, 2014 12:08 am
by UH

Hi

I have done that. But when I test the service, I am getting a test fail with this response

{
"status":"400 Bad Request",
"url":"https://api.appery.io/rest/1/code/904...",
"response":"An error occured"
}


Creating server script

Posted: Thu Aug 07, 2014 4:27 am
by obullei

Hello!

Please check if there are any errors on the tab "trace".
Please send us some screenshots.


Creating server script

Posted: Thu Aug 07, 2014 2:34 pm
by UH

Hi Arina,
The trace said XHRResponse was not defined. Now when I test it, i get this:

Image

It is not displaying the parameters: companyname, phone number, latitude, longitude.

This my script:
var dbId = "53dxxxxxxxxxxxxxxx....(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 = 3959; // Earth radius in miles

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)) {

Code: Select all

 var results = []; 

 var params = {}; 

 var XHRResponse = {}; 

 // Sort the results by company name 

 params.sort = "companyName"; 

 // Get all customers from the database 

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

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

   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) [url=https://d2r1vs3d9006ap.cloudfront.net/s3_images/1085019/database.jpg?1407421625][img]https://d2r1vs3d9006ap.cloudfront.net/s3_images/1085019/database_inline.jpg?1407421625[/img] [/url]

Creating server script

Posted: Thu Aug 07, 2014 2:38 pm
by UH

It didn't post the rest of my code. It is from the tutorial listed in my first post.

I have attached a screenshot of my database collection, customers.

Thanks!


Creating server script

Posted: Thu Aug 07, 2014 11:49 pm
by Yurii Orishchuk

Hello,

Please take a look on the screen shot to understand how you can to debug your server script:

http://prntscr.com/4ampqi/direct

Also in the "trace" tab you can see console.log from the the script. So you can logout in your script things you need.

Also please share your server script with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a and tell us it name.

Regards.