1
votes

Here is a question for parse.com gurus.

I am using Parse Javascript API and trying to execute a query over 2 Pointers and cannot get it to work.

So i have following classes: Posts, Users, Groups. Posts has Pointer to Users. Users has a Pointer to Groups. I need to get all POSTS, where a USER belongs to GROUP, which name starts with "Admin". Here is my code that doesn't work:

var Posts = Parse.Object.extend("Posts");
var Users = Parse.Object.extend("Users");
var Groups = Parse.Object.extend("Groups");

var query = new Parse.Query(Posts);
var innerQueryUsers = new Parse.Query(Users);
var innerQueryGroups = new Parse.Query(Groups);
innerQueryGroups.startsWith("name", "Admin");
innerQueryUsers.matchesQuery("group", innerQueryGroups);

query.matchesQuery("user", innerQueryUsers);
query.find({
  success: function(data) {

  },
  error: function(error){
    // here i get error: {code: 102, message: "bad type for $inQuery"} 
  }
});

Anybody have an idea how to do it right?

1

1 Answers

0
votes

Edit - This can be done in one (untested) query by combining a group query and a post query with the same user query...

function postsInGroup(name) {
    var groupQuery = new Parse.Query("Group");
    groupQuery.equalTo("name", name);
    var userQuery = new Parse.Query(Parse.User);
    userQuery.matchesQuery("group", groupQuery);
    var postQuery = new Parse.Query("Post");
    postQuery.matchesQuery("user", userQuery);
    return postQuery.find();
}

Call it like this...

postsInGroup("Admin").then(function(posts) {
    console.log(JSON.stringify(posts));
}, function(error) {
    console.log(JSON.stringify(error));
});

Its not clear what savings there is between this approach and first querying the group. It's likely that parse.com runs the inner queries much as you would. The difference in readability is a matter of taste.