验证用户名和电子邮件已崩溃,无法插入firebase数据库

时间:2017-09-05 07:59:57

标签: android firebase firebase-realtime-database firebase-authentication

当这行代码对应用程序造成错误时,应用程序无法向数据库插入任何数据。

ref.child(uid).orderByChild("username").equalTo(validateName).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    Toast.makeText(Register.this, "Username Taken. Please try another one", LENGTH_SHORT).show();
                    return;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

此外,uid是代码从电子邮件身份验证部分收集电子邮件。

private FirebaseAuth auth = FirebaseAuth.getInstance();
private FirebaseUser ur = auth.getCurrentUser();
db = FirebaseDatabase.getInstance();
    ref = db.getReference();
    if (ur != null){
        uid = ur.getEmail();
    }

来自logcat的错误

FATAL EXCEPTION: main
                                                                   Process: com.example.rex.ota30, PID: 30205
                                                                   com.google.firebase.database.DatabaseException: Invalid Firebase Database path: rexyou0831@gmail.com. Firebase Database paths must not contain '.', '#', '$', '[', or ']'
                                                                       at com.google.android.gms.internal.zzbqh.zzjm(Unknown Source)
                                                                       at com.google.android.gms.internal.zzbqh.zzjn(Unknown Source)
                                                                       at com.google.firebase.database.DatabaseReference.child(Unknown Source)
                                                                       at com.example.rex.ota30.Register$4.onComplete(Register.java:203)
                                                                       at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

2 个答案:

答案 0 :(得分:1)

正如您的错误所说,Firebase Database paths must not contain '.', '#', '$', '[', or ']'。这意味着Firebase不允许您在键符号中使用这些符号。因此,您需要像这样经济电子邮件地址:

  

name@email.com - >名@电子邮件,玉米

为实现这一目标,我使用以下方法重新命令:

static String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

static String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}

答案 1 :(得分:0)

enter image description here现在我的代码将是这样的,那么将其更改为存在?

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