我试图了解如何使用JavaScript在parse.com的一次调用中查询相关类。

我有2个班级的Post和Comment。 Post具有名为“ Comment”的类型关系列,而Comment具有类型为指针的“ parent”列。

我使用以下基于解析文档的代码来保存帖子和相关评论

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();


我正在尝试查询这样的数据

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);
    }
  });


有人可以告诉我我在做什么错吗?

最佳答案

您的代码中的问题是您没有设置帖子和评论之间的关系。没有这种关系设置,您的查询将无法工作。这是工作代码。请注意,在创建关系之前,我必须保存注释,否则解析时会出错。

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;
    });
})

关于javascript - 一次通话即可查询父子数据-parse.com + JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24917676/

10-09 18:10
查看更多