Support Topics Documentation Slack YouTube Blog

Adding current user to a TABLE Groups


(Alexandre Coutinho) #1

Hi, I’m trying to add users to a group using some kind of “unique value”, for example, my friend creates a group and I want to join it, he will need to send me a Code or URL so I can join it.

I started by creating a Table Groups on Backendless, I added a column “Users” to it, which uses my current Users, but I’m having problems at adding users on, I’ve tried a inviteID that corresponds to a uniqueValue, and I’ve tried the ObjectID, and I’m not having success with neither.

I’ll show some code below:

btnJoinGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(etGroupId.getText().toString().isEmpty()){
Toast.makeText(FirstGroup.this, “Invite EMPTY!”, Toast.LENGTH_SHORT).show();
}
else{
String groupId = etGroupId.getText().toString().trim();
Backendless.Data.of(“Groups”).findById(groupId, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map savedUser) {
Map user = (Map) Backendless.UserService.CurrentUser();
Backendless.Persistence.save(user, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map response) {
Toast.makeText(FirstGroup.this, “Success!!”, Toast.LENGTH_SHORT).show();
}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(FirstGroup.this, “ERROR!”, Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(FirstGroup.this, “ERROU LOGO!”, Toast.LENGTH_SHORT).show();
}
});

}
}
});

}

Thank you for any help you guys are amazing :wink:
Alexandre Coutinho


(Mark Piller) #2

Hi Alexandre,

Here’s the first problem that I see:

Backendless.Data.of(“Groups”).findById(groupId, new AsyncCallback<Map>() {
@Override
public void handleResponse(Map savedUser) {

Notice that you make a request to the Groups data table, and in the handleResponse you call the argument savedUser. This is wrong, since the response you get back would be a Map representing a group object.

Then in the same handleResponse callback, you do the following:

Map user = (Map) Backendless.UserService.CurrentUser();
Backendless.Persistence.save(user, new AsyncCallback<Map>() {

Well, that will do absolutely nothing useful. You take the currently logged in user and you just save it back into the database. What is that for?

Cheers,
Mark


(Alexandre Coutinho) #3

I was trying to save the current user on the group that corresponds to the input code, I’m still having a lot of trouble here. The current user should be able to enter a given code (that corresponds to a group) and join that group.

Thanks Mark!
Alex


(Mark Piller) #4

Okay, Alex. What would be the design at the data level? Do you understand how you could use table relations here?


(Alexandre Coutinho) #5

Hi Mark,
No I don’t think I know how to use table relations…


(Andrii Bodnar) #6

Hi, Alexandre. If you want to associate current user with some group, existing in the corresponding table, you should use one of the next methods:

Backendless.Data.of( E ).addRelation( 
                                       E parentObject,
                                       String relationColumnName,
                                       Collection<T> children,
                                       AsyncCallback;Preformatted text<Integer> callback );

Backendless.Data.of( "TABLE-NAME" ).addRelation( 
                                                 Map parentObject,
                                                 String relationColumnName,
                                                 Collection<Map> children,
                                                 AsyncCallback<Integer> callback );

More information in SDK Documentation.

Feel free to ask more questions.