Validate username and email crashed and cannot insert to firebase database

后端 未结 2 866
感动是毒
感动是毒 2021-01-25 19:59

The apps cannot insert any data to the database while this line of code makes error to the apps.

ref.child(uid).orderByChild(\"username\").equalTo(validateName)         


        
相关标签:
2条回答
  • 2021-01-25 20:23

    As your error said, Firebase Database paths must not contain '.', '#', '$', '[', or ']'. This means that Firebase does not allow you use in the key symbols those symbols. Because of that, you need to econde the email address like this:

    name@email.com -> name@email,com

    To achieve this, i recomand you using the following methods:

    static String encodeUserEmail(String userEmail) {
        return userEmail.replace(".", ",");
    }
    
    static String decodeUserEmail(String userEmail) {
        return userEmail.replace(",", ".");
    }
    
    0 讨论(0)
  • 2021-01-25 20:28

    Now currently my code will be like this so where to change that to exists?

    ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
    
                        for(DataSnapshot c : dataSnapshot.getChildren()){
    
                            String vun = c.child("username").getValue().toString();
    
                            if (vun.equalsIgnoreCase(usnm)){
    
                                Toast.makeText(Register.this, "Username Taken. Please try another one.", LENGTH_SHORT).show();
                                return;
    
                            }
                        }//end of for loop
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
    
    
                    }
                });// end of addvalue
    
    0 讨论(0)
提交回复
热议问题