1

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.

1 Answer 1

1

No need to do an additional query on the UserInGroup's User attribute. Those can be fetched eagerly using include(), as follows:

// return a promise that is fulfilled with an array of users belonging to
// the Group with id groupId
function usersInGroup(groupId) {
    var groupQuery = new Parse.Query("Groups");
    return groupQuery.get(groupId).then(function(group) {
        var userInGroupQuery = new Parse.Query("UserInGroup");
        userInGroupQuery.equalTo("Group", group);
        // eagerly fetch the related user...
        userInGroupQuery.include("User");
        return userInGroupQuery.find();
    }).then(function(results) {
        var users = [];
        for (var i = 0; i < results.length; i++) { 
            var object = results[i];
            users.push(object.get("User"));
        }
        return users;
    });
}

Call it like this:

usersInGroup(someGroupId).then(function(users) {
    for (var i = 0; i < users.length; i++) { 
        var user = users[i];
        console.log(user.getUserName());  // or get("username")
    }
}, function(error) {
    console.log(error);
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you it works after correcting an error at the call
Glad to help. Sorry about the copy paste error on the loop. Will fix. But I think getUsername() is the correct method to get a user's username. Either that or .get("username") works with all lowercase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.