javascript - Parse Cloud Code fetch pointer


Keywords:javascript 


Question: 

I'm working on an app where I'd like to implement radial search. For this, I created a class on Parse called "UserLocation", where I save the users coordinates and a pointer to the "_User" Object. Now I'm working on a cloud code function to get some users within a radius. With the following code, I'm getting the near people but unfortunately only with a pointer, so I would need to fetch all those users on the device. Is there a way to get the User-Objects from the cloud code instead of the pointer?

Parse.Cloud.define('circum_search', function (request, response)
{
    var lat = request.params.lat;
    var lon = request.params.lon;
    var radius = request.params.radius;
    var query = new Parse.Query("UserLocation");
    query.withinKilometers("location", new Parse.GeoPoint(lat, lon), radius);
    query.find({
        success: function(locationObjects) {
            Parse.Object.fetchAllIfNeeded(locationObjects);   
            response.success(locationObjects);
        }
    });
});

The answer I get now is an array of objects like this:

 {
  "location": {
    "__type": "GeoPoint",
    "latitude": 47.3268966,
    "longitude": 8.542694
  },
  "user": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "ce9GRAzCF8"
  },
  "createdAt": "2018-02-01T17:08:07.078Z",
  "updatedAt": "2018-02-01T17:08:07.078Z",
  "ACL": {
    "d9db3aUdYU": {
      "read": true,
      "write": true
    },
    "*": {
      "read": true
    }
  },
  "objectId": "m3DmYfFv7I",
  "__type": "Object",
  "className": "UserLocation"
}

The Goal I have now is to replace the object for the key "user" with the real user-object, not the pointer.


2 Answers: 

FYI You can also use the include() method of queries with dot notation to also include the objects stored as pointers on an included object. I.e. if ClassA has a pointer to ClassB has a pointer to ClassC, you can do this:

var query = new Parse.Query("ClassA");
query.include("classB").include("classB.classC");

Assuming the fields are named using lowercase classname.

 

Ok, finally there's a very simple solution but I had a long time to find it ;) just add

query.include('user')

where user is the name of the field with the Pointer