1

I have a class with multiple pointers, which I want to return their full objects in the response (like a join in SQL). The example below does that, but it returns JSON instead of a Parse object. How do I return an array of objects which contain each Parse object from the pointers WITHOUT doing an additional query for each pointer?

    query.find().then(function(results){
        /* Go Through Each Comment*/
        var commentsArray = new Array();
        for(i in results){
            /* Set obj to current comment*/
            var obj = results[i];
            /* Get Post's Name */
            var postName = obj.get("post").get("postName");
            /* Get Post's Message */
            var postMsg = obj.get("post").get("postMsg");
            /* Get Post Author's Name */
            var authorName = obj.get("post").get("postAuthor").get("name");
            /* Get Comment's Message */
            var commentMsg = obj.get("msg");
            /* Get Comment's Author*/
            var commentAuthor = obj.get("user").get("name");

            /* Let's Put the Comment Information in an Array as an Object*/
            commentsArray.push({
                post:{
                    name: postName,
                    msg: postMsg
                },
                author: authorName,
                comment: {
                    author: commentAuthor,
                    msg: commentMsg
                }
            });
        }
    })

Edited (I'm building with Swift on the client):

var query = new Parse.Query("Profile");

query.equalTo("objectId", objectId);

query.find().then(function(profile) {
  response.success(profile)  // Returns profile parse object
}, function(error) {
  response.error(error);
});


//RETURNS
"<Profile: 0x153f96570, objectId: HKdukNBlmA, localId: (null)> {\n}"



Parse.Cloud.define("getProfiles", function(request, response) {
  var query = new Parse.Query("Profile");

  query.include("friendId");

  query.find().then(function(profiles) {
    var res = [];

    profiles.forEach(function(profile) {
      var obj = {
        firstName: profile.get("firstName"),
        lastName: profile.get("lastName"),
        age: profile.get("age"),
        gender: profile.get("gender")
      };

      obj.friend = {
        firstName: profile.get("friendId").get("firstName"),
        lastName: profile.get("friendId").get("lastName"),
      };

      res.push(obj);
    });

    response.success(res);
  }, function(error) {
    response.error("Error: " + error.code + " " + error.message);
  });
});


// RETURNS
[{
  firstName: "bob",
  lastName: "smith",
  age: 19,
  gender: male
  friend: {
    firstName: "tim",
    lastName: "wang",
  }
},
{
  firstName: "chris",
  lastName: "scalia",
  age: 24,
  gender: male
  friend: {
    firstName: "ben",
    lastName: "calder",
  }
}]

I prefer the former.

4
  • Can you add the code that sets up the query, and can you show the results somehow (like via log output) and explain how they are not what you expect? The idea of getting "JSON instead of an object" is funny to me. JSON is a string representation of objects that have been serialized. Whether these are parse objects depends on what the JSON says. Commented Jan 22, 2016 at 23:31
  • Another way to put it: if var postName = obj.get("post").get("postName"); gives you a nice string in postName, and not an error, then your code works. You have the post object and don't need another fetch. Commented Jan 22, 2016 at 23:33
  • @danh - Check out my update. Commented Jan 23, 2016 at 0:06
  • @danh I prefer to return an array of Parse objects to the client (iOS - Swift). Ex: ( "<Tour: 0x153f96570, objectId: HKdukNBlmA, localId: (null)> {\n}", "<Tour: 0x1541e6710, objectId: lodxwPR3rz, localId: (null)> {\n}", "<Tour: 0x1543687e0, objectId: BBHj6BVQo9, localId: (null)> {\n}" ) Commented Jan 23, 2016 at 0:14

1 Answer 1

0

The caller is getting partial objects (just some attributes) back because the cloud function responding with a handful of attributes that it constructs. To get the parse objects back, return the parse objects...

Parse.Cloud.define("getProfiles", function(request, response) {
    var query = new Parse.Query("Profile");
    query.include("friendId");
    query.find().then(function(profiles) {
      response.success(profiles);
    }, function(error) {
      response.error("Error: " + error.code + " " + error.message);
    });
});

Of course, this doesn't need to be a cloud function since it isn't adding any value to the query. The client could just run the query.

Also note, the other thing I've seen, especially by swift authors calling parse, is the strange habit of making a query, getting an array of PFObjects in response, but then iterating those objects and storing only some of the attributes in a local array. This is generally a mistake, so, in my opinion, you're client should look like this...

// no need to call a cloud function that just does a query
var query = PFQuery(className:"Profile")
query.includeKey("friendId")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
    if error == nil {
        // don't iterate the objects here, saving bits of them in an array
        // have an instance var that is an array of PFObjects
        self.objects = objects;
    } else {
        print("Error: \(error!) \(error!.userInfo)")
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.