如何在Parse中将不同表的两个查询结果合并为一个查询?

时间:2016-06-01 06:01:08

标签: javascript parsing parse-platform

我正在尝试使用关系查询。我有' Post'表和'评论'表。我希望所有评论都有来自Post表的post_id以及与Post表中的post_id相关的所有帖子。我在这里制作了post_id指针。到目前为止,我所做的是我能够获得具有该post_id的评论,但我无法使用该post_id从该Post表中获取帖子。在这里,我到目前为止所做的是:



Parse.Cloud.define("getAllPost", function (request, response)
{           
  var Post = Parse.Object.extend("mst_Post");
  var Comment = Parse.Object.extend("mst_Comment");
  var innerQuery = new Parse.Query(Post);
  innerQuery.exists("objectId");

  var query = new Parse.Query("mst_Comment");
  query.matchesQuery("post_id", innerQuery);
query.find
({
  success: function(result) 
  {
      response.success(result);
  }
});  




如何使用此帖子获取帖子,其中包含Post表中的相同post_id。任何帮助将不胜感激。注意:请不要建议query.or,因为它只从同一个表中获取数据,在我的情况下,有两个不同的表。注意:



error : code": 141,
    "error": "Error: Tried to encode an invalid date.  for (j = 0; j < commentResult.length; j++) 
                        {
                            var comment_post_id = commentResult[j].get("post_id").id;
                           var comment_date = commentResult[j].createdAt;  
                            var extract_Date =new Date(comment_date.iso) 
                            var comment_id = commentResult[j].id;
                            if (comment_post_id == post_id) {
                                var tempArr = {
                                    comment_id : comment_id,
                                    comment: commentResult[j].get("comment"),
                                    comment_location: commentResult[j].get("comment_location"),
                                    Comment_Date :  extract_Date
                                }
                                commentArr.push(tempArr);
                            }
                        }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

总结你想要的东西。

您想获得评论及其帖子。 每条评论都有一个名为'post_id'的指针

只需使用include,这样parse就会在相关字段中获得相关的帖子数据。

Parse.Cloud.define("getAllPost", function (request, response)
{           
  var Post = Parse.Object.extend("mst_Post");
  var Comment = Parse.Object.extend("mst_Comment");
  var innerQuery = new Parse.Query(Post);
  innerQuery.exists("objectId");

  var query = new Parse.Query("mst_Comment");
  query.matchesQuery("post_id", innerQuery);
  query.include("post_id");
query.find
({
  success: function(result) 
  {
      //console.log(result[0].get('post_id').get('XXX in post'));
      response.success(result);
  }
});  

参考 https://parse.com/docs/js/guide#queries-relational-queries

更新了部分

如果你想获得Paras.Object的createdAt,只需使用 obj.createdAt 而不是使用obj.get(“creadtedAt”)。

如果要将结果转换为日期类型,

新日期(obj.get(“Comment_Date”)。iso)

Comment_Date.iso中的字符串可转换为日期对象