Reputation: 77
I want to check if user is the member of a specific team, if user isn't he will then be ask to join, if he is, then he will be directed to a page. I did find some of the similar question but for somehow I still couldn't add user to the database. Below is my code and this code crash whenever i click on one of the list.
databaseMembers = FirebaseDatabase.getInstance().getReference("members");
listViewTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//getting the selected team
Team team = teams.get(i);
checkMember();
//creating an intent
Intent intent = new Intent(getApplicationContext(), ChatRoomActivity.class);
//putting team name and id to intent
intent.putExtra(TEAM_ID, team.getTeamId());
intent.putExtra(TEAM_NAME, team.getTeamName());
//starting the activity with intent
startActivity(intent);
}
});
private void checkMember(){
FirebaseUser user = firebaseAuth.getCurrentUser();
final String teamMember = user.getEmail();
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(teamMember)){
finish();
}
else{
databaseMembers.push().setValue(teamMember);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Logcat:
04-07 12:44:41.167 19091-19091/com.example.user.stfv2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.stfv2, PID: 19091
com.google.firebase.database.DatabaseException: Invalid Firebase Database path: [email protected]. Firebase Database paths must not contain '.', '#', '$', '[', or ']'
at com.google.android.gms.internal.zzbtf.zzjl(Unknown Source)
at com.google.firebase.database.DataSnapshot.hasChild(Unknown Source)
at com.example.user.stfv2.MainActivity$8.onDataChange(MainActivity.java:352)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
My Database:
When user click on "heng" team, it will check if user's email exist in the database, if isn't it will then add user's email as a teamMember. I can add the user who created the team, but I can't seem to add users who wants to join later. Really appreciate if someone could help me. Let me know if there's lack of information.
Upvotes: 0
Views: 566
Reputation: 197
You problem is written your logcat:
Firebase Database paths must not contain '.', '#', '$', '[', or ']'
But I also look over your code. You can write firebase Query like this.
databaseMembers = FirebaseDatabase.getInstance().getReference("members");
Query q = databaseMembers.orderbyChild("heng").equalTo(user.getEmail());
You can write dynamic I just give a example.
Upvotes: 0
Reputation: 7391
You problem is clearly mentioned in your logcat:
Firebase Database paths must not contain '.', '#', '$', '[', or ']'
I think it doesn't even accept '@'. The proper way to add your user into the DB is to use users uid.
Eg:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
In your code you can do something like this:
private void checkMember(){
FirebaseUser user = firebaseAuth.getCurrentUser();
final String teamMember = user.getUid();
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(teamMember)){
finish();
}
else{
databaseMembers.push().setValue(teamMember);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Upvotes: 0
Reputation: 138824
The problem is that Firebase does not accept .
(dot) symbol in the key. So in order to use an email address, you need to have an encoding method. The email addres must be name@mail,com
(see the ,
comma) in stead of [email protected] as it is in your case. These are the methods to encode and decode the email address.
static String encodeUserEmail(String userEmail) {
return userEmail.replace(".", ",");
}
static String decodeUserEmail(String userEmail) {
return userEmail.replace(",", ".");
}
This methods will solve your problem.
Upvotes: 1