简体   繁体   中英

Parse - get parent items with their child items in array one to many relation

I have the following relation in my Parse schema: "Post" items with "Comments" child items. I'm Using Arrays for One-to-Many Relations as described here: relations_guide

How can i fetch all Posts (even those with no comments) with all the comments for each post?

I was trying something like this:

var post = Parse.Object.extend("post");
var comment = Parse.Object.extend("comment");

var queryPosts = new Parse.Query(post);
var queryComments = new Parse.Query(comment);

queryPosts.matchesQuery("commentsList", queryComments);

return queryPosts.find().then(function (postsResult) {

console.log('prepareResponses: number of posts found: ' + postsResult.length);

and then, of course, I would like to get the comments out of the post like this maybe?

p = postsResult[0].get("commentsList");
console.log("Number of comments in the first post is: " + p.length);

I you want to query for all rows in a particular class (assuming comments is a relations object in post):

var post = Parse.Object.extend("post");
var postQuery = Parse.Query(post);
postQuery.includeKey("comments");
//post query will return all rows in post class with the comments relation loaded

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