I have a problem with a many-to-many relationship on parse.com.
First of all I have two classes (Groups and Users) and a class (UserInGroup) to realize the m-to-m relationship.
Class structure:
Groups: objectId(string) -- GroupName(string) -- ...
Users: objectId(string) -- UserName(string) -- ...
UserInGroup: objectId(string) -- Group(Pointer< Groups>) -- User(Pointer< Users>)
How can I get the UserName of alle Users who are in a specificGroup (I know the objectId of the Group).
I trieds this
function getUserNameOfGroup(GroupID){
var UserInGroup = Parse.Object.extend("UserInGroup");
var query = new Parse.Query(UserInGroup);
var usersArray = [];
var Groups = Parse.Object.extend("Groups");
var collectionQuery = new Parse.Query(Groups);
collectionQuery.get(GroupID, {
success: function(group) {
query.equalTo("Group", group);
var usersArray = [];
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var user = object.get("User");
user.fetch({
success: function(tempUser) {
usersArray.push(user.get("UserName"));
}
}).done(function() {
if(results.length == usersArray.length){
//do something with the array
}
});
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
},
error: function(object, error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
For testing I put
if(results.length == usersArray.length){
for (var i = 0; i < usersArray.length; i++) {
alert(usersArray[i]);
}
}
in the done section. The first entry of usersArray is always "undefinde".
What am I doing wrong? Or rather, is there an easier, more beautiful and faster way to get the array? I'm sure there's a better solution ;).
thanks for help.