简体   繁体   中英

How to combine two queries result of different tables into one query in Parse?

I am trying to use relational query here . I have 'Post' table and 'Comment' table. I want all the comments having post_id from the Post table and also all the post related to that post_id from Post table. I have made post_id pointer here. As far, what I have done is I am able to get comments having that post_id, but I am not able to get post from that Post table using that post_id. Here, is what I have done so far :

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

How do I get posts also using this, containing that same post_id from the Post table. Any help would be appreciated .Note : Please don't suggest query.or , as it takes data from the same table only, and in my case there are two different tables. Note :

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

Summarize what you want.

You want to get comments and its post. Every comment have a pointer named 'post_id'

Just using include, so that parse will get related post data inside the related field.

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

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


Updated Part

If you want to get createdAt of Paras.Object, just use obj.createdAt not use obj.get("creadtedAt").

If you want to transform the result to Date type,

new Date(obj.get("Comment_Date").iso)

the String in Comment_Date.iso is convertable to Date Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM