Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to understand how to query related classes in one call from parse.com using javascript.

I have 2 classes Post and Comment. Post has a column of type relation named 'Comment' and Comment has a column 'parent' of type pointer.

I use the following following code based on parse docs, to save a post and related comment

var Post = Parse.Object.extend("Post");
var Comment = Parse.Object.extend("Comment");

//Create the post
var myPost = new Post();
myPost.set("title", "I'm Hungry");
myPost.set("content", "Where should we go for lunch?");

// Create the comment
var myComment = new Comment();
myComment.set("text", "Let's do subway.");

myComment.set("parent", myPost);     
myComment.save();

I am trying to query data like this

var query = new Parse.Query("Post");
query.descending("createdAt");

query.find({
    success: function(results) {
      for(var i = 0; i < results.length; i++) {
        var post = results[i];
        var relation = post.relation('Comment');

        relation.query().find({
          success: function(comments) {
            for(var j = 0; j < comments.length; j++) {
              console.log(comments[j].get("content"));
            }
          }
        });
      }
    },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
    }
  }); 

Can anybody tell me what i am doing wrong here?

share|improve this question

1 Answer 1

The problem in your code is that you're not setting up the relation between the post and the comment. Without that relation setup your query won't work. Here's the working code. Note that I have to save the comments before creating the relation, otherwise you'll get an error from parse.

function createPost() {
    var Post = Parse.Object.extend("Post");
    var Comment = Parse.Object.extend("Comment");

    //Create the post
    var myPost = new Post();
    myPost.set("title", "I'm Hungry");
    myPost.set("content", "Where should we go for lunch?");

    // Create the comment
    var comment1 = new Comment();
    comment1.set("text", "Let's do subway.");
    comment1.set("parent", myPost);     

    var comment2 = new Comment();
    comment2.set("text", "Let's do Hoagies.");
    comment2.set("parent", myPost);     

    return Parse.Object.saveAll([comment1, comment2]).then(function() {
        // Set the relationship
        var relation = myPost.relation('comments');
        relation.add(comment1);
        relation.add(comment2);
        return myPost.save();
    });
}

createPost().then(function() {
    var query = new Parse.Query("Post");
    query.descending("createdAt");
    var posts = [];

    return query.find().then(function(results) {
        var promises = [];

        for(var i = 0; i < results.length; i++) {
            var post = results[i];
            var relation = post.relation('comments');
            var postData = { post : post, comments : [] };

            posts.push(postData);
            console.log("Post : " + post.get('title'));

            promises.push(relation.query().find({
                success: function(comments) {
                    for(var j = 0; j < comments.length; j++) {
                        console.log("Comment " + j + " : " + comments[j].get("text"));
                        postData.comments.push(comments[j]);
                    }
                }
            }));
        }
        return Parse.Promise.when(promises);
    },
    function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
    ).then(function() {
        console.log(posts);
        return posts;
    }); 
})
share|improve this answer
    
Thanks It works. Now how do i combine all of this data into one array in the following format [{post1 , array(comments_for_post1)},{post2, array(comments_for_post2)}] - thanks –  user3869405 Jul 23 at 21:25
    
We're starting to get off-topic here but I updated the code above to do what you want. You can accept the answer if you think it's correct. –  Troy Sandal Jul 24 at 1:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.