验证用户名和电子邮件崩溃并且无法插入到 firebase 数据库 [英] Validate username and email crashed and cannot insert to firebase database
问题描述
应用程序无法向数据库插入任何数据,而这行代码使应用程序出错.
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).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 是从电子邮件身份验证部分收集电子邮件的代码.
Besides, the uid is the code gather the email from the email authentication part.
private FirebaseAuth auth = FirebaseAuth.getInstance();
private FirebaseUser ur = auth.getCurrentUser();
db = FirebaseDatabase.getInstance();
ref = db.getReference();
if (ur != null){
uid = ur.getEmail();
}
来自 logcat 的错误
The error from 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)
推荐答案
正如您的错误所说,Firebase 数据库路径不得包含."、#"、$"、["或]'代码>.这意味着 Firebase 不允许您在键符号中使用这些符号.因此,您需要像这样对电子邮件地址进行编码:
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
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(",", ".");
}
这篇关于验证用户名和电子邮件崩溃并且无法插入到 firebase 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!