Search code examples
javascriptiosswiftparse-platformparse-cloud-code

Parse Cloud Code - Only Retrieve Certain Columns Before Sending Response.


I currently have this cloud code to retrieve all the users that have a relation with the current user. I have to first find the current user then query the "Friends" column hence the relational query step.

The line response.success(results) returns all the attributes of all of the current user's friends. I only want a few of the columns that belong to these friends, not every single thing they saved when signing up.

    Parse.Cloud.define("getFriends", function(request, response) {
    var userId = request.params.UserKey;
    var query = new Parse.Query(Parse.User);
    query.ascending("updatedAt");
    query.get(userId, {
        success: function(foundCurrentUser) {
            var currentUser = foundCurrentUser;
            var relation = currentUser.relation("Friends");
            var getRelationQuery = relation.query();
            getRelationQuery.find().then(function(results) {


                response.success(results);

            });
        },
        error: function(error) {
            response.error(error);
        }
    });
});

I am using swift to to use the response, I am not sure that if I need to tweak the swift code but will provide it anyway.

func LoadCarpoolersFromParse(Success:(object:AnyObject)->(),Failure:(error:NSError)->())
{
        let params = NSMutableDictionary()
        params.setObject(PFUser.currentUser()!.objectId!, forKey: "UserKey")

        PFCloud.callFunctionInBackground("getCarpoolers", withParameters: params as [NSObject : AnyObject], block: {
            (response:AnyObject?, error: NSError?) -> Void in
            if error == nil {
                Success(object: response!)
            }
            else{
                Failure(error:error!)
            }
        })
    }
}

Solution

  • You can do it by using select method of Parse.Query, make the following changes in your cloud code

    Parse.Cloud.define("getFriends", function(request, response) {
    var userId = request.params.UserKey;
    var query = new Parse.Query(Parse.User);
    query.ascending("updatedAt");
    query.select("name","phone"); // replace with your required fields