Hi Arina,
The trace said XHRResponse was not defined. Now when I test it, i get this:
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]