Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

Hi
Using server code I can query the Appery Mongo DB with...

Collection.query(dbId, "tablename", params);

How would I query an external rest database this way from jscript?
Thank you

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

Querying an external database

Hello Deon,

Please use XHR requests for that: https://docs.appery.io/reference#serv...
If your database doesn't have a REST API, please use API Express to generate it: https://docs.appery.io/docs/apiexpres...

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

Perfect. Thank you.

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

Hi

There seems to be a problem with the server code. This is server code which I have been using and it used to work fine with an appery collection.
Now it fails.

This is my server code error when I test the Sever Code

Script ZootRides: TypeError: Cannot get property "latitude" of null

I have entered the parameters

Here is my server code.

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(dlat,plat,dlon,plon) {
var R = 6371; // Radius of the earth in km
var disLat = deg2rad(plat-dlat); // deg2rad below
var disLon = deg2rad(plon-dlon);
var a =
Math.sin(disLat/2) * Math.sin(disLat/2) +
Math.cos(deg2rad(slat)) * Math.cos(deg2rad(plat)) *
Math.sin(disLon/2) * Math.sin(disLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}

function deg2rad(deg) {
return deg * (Math.PI/180);
}

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 url = "[url=http://129.232.161.106:81/api/v2/zoot/_tables/rides]http://129.232.161.106:81/api/v2/zoot...[/url]"; // REST API URL 

var rides = XHR2.send("GET", url, {
"headers": {
"api_key": "83edb3c5b591..........................................................79368f8e"
}
});

Code: Select all

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

   // Get coordinates from the response 
   var lat = Number(rides[i].startLAT); 
   var lng = Number(rides[i].startLNG); 
   var Distance = getDistance(lat, latitude, lng, longitude); 

   // If the customer's coordinates are within the radius, 
   // add the customer record to the results array 
   if (getDistance(lat, latitude, lng, longitude) , 400); 

}

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

PS.

I copied this from my old server code which connects to an appery db and just changed it to connect to my REST database.

However, the server code sample supplied by Appery to connect to an Appery collection does not work either anymore. This always used to work and has suddenly stopped working.

I think you guys have changed things again and therefor code is invalid.

Please can you assist with this issue.

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

Also

Must this be server code. Can I not implement this code locally somehow?

Thank you.

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

Could this be because I am trying to access a non https REST service from your server?

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

Querying an external database

If you want us to help you debug the app, please consider purchasing the Advisory Pack service: http://appery.io/service. This service includes app debugging as well as any other custom app help. If this particular issue turns out to be an Appery.io bug, we will credit back the hours spent on this issue and you will be able to use them toward another issue.

If you want to run these requests from the application, instead of Server Code - please use an API Express for that: https://docs.appery.io/docs/apiexpres...

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

No.

Do I need to use https or can I use http for below?

var url = "http://129.232.161.106:81/api/v2/zoot..."; // REST API URL
var rides = XHR2.send("GET", url, {
"headers": {
"api_key": "83edb3c5b591..........................................................79368f8e"
}
});

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Querying an external database

http://appery.io/service does not exist.

As as I said many times. API Express does not work in my country (South Africa) due to the high latency. API Express is NOT an option.

Return to “Issues”