Skip to content Skip to sidebar Skip to footer

Simple Way To Get Multiple Related Objects In Parse.com With Javascript?

I have a Player class. Players can have x number of Trophies. I have the Player objectId and need to get a list of all of their Trophies. In the Parse.com data browser, the Player

Solution 1:

I finally sorted this out, though there is a caveat that makes this work differently than the documentation would indicate.

//Assuming we have 'player', an object of Class 'Player'.var r = player.relation("trophies");
r.query().find({
  success: function(trophies){
    response.success(trophies); //list of trophies pointed to by that player's "trophies" column.
  },
  error: function(error){
    response.error(error);
  }

})

The caveat: You must have a 'full' player object in memory for this to work. You can't save a player object, grab the object from the success callback and have this work. Some reason, the object that is returned in the success handler is appears to be an incomplete Parse.Object, and is missing some of the methods required to do this.

Another stumbling block about the Parse.com Javascript SDK- A query that finds nothing is still considered successful. So every time you query for something, you must check the length of the response for greater than 0, because the successful query could have returned nothing.

Solution 2:

This is what worked for me:

var Comment = Parse.Object.extend("Comment");
        var commentsQuery = new Parse.Query(Comment);
        commentsQuery.equalTo("parent", video);//for the given 'video' instance, find its children (comments)
        commentsQuery.descending("createdAt");
        return commentsQuery.find();

Note: of course, video is an instance of the Video class. And returning find() means I'll have to handle the 'promise' in whatever function calls this one.

And here is another function coming from the other angle:

    getRecentCommentsOfAllVideos: function(limit) {
        var Comment = Parse.Object.extend("Comment");
        var commentsQuery = new Parse.Query(Comment);
        commentsQuery.descending("createdAt");
        commentsQuery.limit(limit);
        commentsQuery.include("parent");//this enables the details of the comment's associated video to be available in the resultreturn commentsQuery.find();
    }

(Be sure to read https://parse.com/docs/js_guide and search for the line that says "Include the post data with each comment".)

I also looked at these materials; maybe they'll help you (though they weren't much help for me):

Post a Comment for "Simple Way To Get Multiple Related Objects In Parse.com With Javascript?"