Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Javascript operation on negative value variable in DB Query failing

Hi,

To start I have near zero experience with javascript.

For some reason whenever I perform a calculation on a negative variable my query fails. When I perform the a calculation on a negative number it succeeds. I provide examples below.

Using geolocation I pull down the current latitude and longitude and then write the data to my database.These values are stored in localStorage so that I can then call them to do a DB query based on their values. The examples below involve pulling the current location from the localStorage variables within the WHERE clause of a DB Query.

The Lat (latitude) value for my location is 32, the Long (longitude) value is -117.

code
SUCCESS (returns all users with lat (31 to 33) and long (-117))

var lat = localStorage.getItem('currentLat');
var long = localStorage.getItem('currentLong');

var max_lat = lat + 1;
var min_lat = lat - 1;

var max_long = long;
var min_long = long;

return '{ $and:[ {"Latitude":{"$lte":'+max_lat+'}},{"Latitude":{"$gte":'+min_lat+'}},{"Longitude":{"$lte":'+max_long+'}},{"Longitude":{"$gte":'+min_long+'}} ] }';
/code

code
FAIL (returns nothing)

var lat = localStorage.getItem('currentLat');
var long = localStorage.getItem('currentLong');

var max_lat = lat + 1;
var min_lat = lat - 1;

var max_long = long + 1;
var min_long = long - 1;

return '{ $and:[ {"Latitude":{"$lte":'+max_lat+'}},{"Latitude":{"$gte":'+min_lat+'}},{"Longitude":{"$lte":'+max_long+'}},{"Longitude":{"$gte":'+min_long+'}} ] }';
/code

code
SUCCESS (returns users with lat (31 to 33) and long (-116 to -118)

var lat = localStorage.getItem('currentLat');
var long = localStorage.getItem('currentLong');

var max_lat = lat + 1;
var min_lat = lat - 1;

var max_long = -117 + 1;
var min_long = -117 - 1;

return '{ $and:[ {"Latitude":{"$lte":'+max_lat+'}},{"Latitude":{"$gte":'+min_lat+'}},{"Longitude":{"$lte":'+max_long+'}},{"Longitude":{"$gte":'+min_long+'}} ] }';
/code

Please let me know if you have any ideas on why this seemingly simple operation appears to be failing.

Return to “Issues”