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

标签 javascript parsing parse-platform

我尝试在这里使用关系查询。我有“帖子”表和“评论”表。我想要 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);
                            }
                        }

最佳答案

总结你想要什么。

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

只需使用 include,以便解析将获取相关字段内的相关帖子数据。

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")。

如果要将结果转换为Date类型,

新日期(obj.get("Comment_Date").iso)

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

关于javascript - 如何在Parse中将不同表的两个查询结果合并为一个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560568/

相关文章:

javascript - AJAX:如何在初始加载时加载内部网页?

c - bison输出文件编译时 `main'的多重定义

ios - Segue 不将数据传递给 ViewController

ios - 如何使用 OAuth 对 Parse iOS SDK 中的用户进行身份验证?

javascript - 如何使用 AngularJS 加载函数信息并将其保存在路由中的解析范围内

javascript - 这里为什么需要分号?

javascript - 将 HTMLDocument 转换/解析为 HTMLElement

使用 HTMLHelper 和 "All"选择下拉列表值的 JavaScript 在 ASP .NET MVC4 中不起作用

Python从字符串中解析json

python - NLTK CFG 多个单词语法