Jason
Jason

Reputation: 2353

Parse CloudCode invalid geopoint object

I'm using the JavaScript SDK in Parse Cloud Code, attempting to write a query that filters off of a GeoPoint. I keep getting the error:

{"code":141,"error":" Error: 102 $nearSphere: invalid geopoint object"}

My code looks like this:

Parse.Cloud.define("findPeopleNearby", function (request, response) {
Parse.Cloud.useMasterKey();
if (request.params.latitude && request.params.longitude) {
    var point = new Parse.GeoPoint({ latitude: request.params.latitude, longitude: request.params.longitude });
    console.log('would query off of GeoPoint: ' + JSON.stringify(point));

    var places = Parse.Object.extend("UserLocation");
    var placesQuery = new Parse.Query(places);
    placesQuery.near("location", point);

    placesQuery.find({
        success: function (results)
        {
            console.log("Successfully retrieved " + results.length + " rows.");
            response.success();
        },
        error: function (error)
        {
            response.error("Error: " + error.code + " " + error.message);
        }
    });
}});

I know that the data in the table is okay, because I can issue a curl command with success:

curl -X GET -H "X-Parse-Application-Id: xxxx" -H "X-Parse-REST-API-Key: yyyy" -G --data-urlencode 'limit=5' --data-urlencode 'where={ "location": { "$nearSphere": { "__type": "GeoPoint", "latitude": 37.33521504  , "longitude": -122.03254905 }, "$maxDistanceInMiles":10.0 } }'  https://api.parse.com/1/classes/UserLocation

I originally had the location property on the Installation table (which is where I would prefer it to be), but I saw this Q&A and thought that I would try it in its own object, but that did not seem to make a difference.

As an aside, if I can get this query to work, my next attempt would be to join the Installation object using a parent pointer. I hope/assume that that would work if I can understand what is causing this error.

Thanks

EDIT console output

would query off of GeoPoint: {"__type":"GeoPoint","latitude":"37.33521504","longitude":"-122.03254905"}

Upvotes: 0

Views: 957

Answers (1)

Jason
Jason

Reputation: 2353

Timothy's suspicions were correct. The inputs were strings and not floats. Casting the inputs solves the error and, in fact, querying to "join" the installation object then works fine as well. A silly error, but hopefully now anyone Googling it will find a result to point them in the right direction.

Long version for reference:

        var lat, lng;

    if (typeof request.params.latitude === "number") {
        lat = request.params.latitude;
    }
    else {
        lat = parseFloat(request.params.latitude);
        // this could return NaN
    }

    if (typeof request.params.longitude === "number") {
        lng = request.params.longitude;
    }
    else {
        lng = parseFloat(request.params.longitude);
    }

    var point = new Parse.GeoPoint({ latitude: lat, longitude: lng });

Upvotes: 2

Related Questions