我要将电子邮件地址和密码存储到Firebase中,并且希望将电子邮件地址存储为主键,但应用程序崩溃并显示:
无效的Firebase数据库路径:ccw@qq.com。 Firebase数据库路径 不得包含“。”,“#”,“ $”,“ [”或“]”
private DatabaseReference Userdatabase;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
private static final String TAG = "SignupActivity";
EditText nameText, addressText, emailText, mobileText, passwordText, reEnterPasswordText;
Button signupButton;
TextView loginLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
nameText = (EditText) findViewById(R.id.input_name);
addressText = (EditText) findViewById(R.id.input_address);
emailText = (EditText) findViewById(R.id.input_email);
mobileText = (EditText) findViewById(R.id.input_mobile);
passwordText = (EditText) findViewById(R.id.input_password);
reEnterPasswordText = (EditText) findViewById(R.id.input_reEnterPassword);
firebaseAuth = FirebaseAuth.getInstance();
signupButton = (Button) findViewById(R.id.btn_signup);
loginLink = (TextView) findViewById(R.id.link_login);
Userdatabase = FirebaseDatabase.getInstance().getReference("User");}
public void AddUser(final String UserEmail, final String Username, final String Password,
final String PhoneNumber, final String ComfirmPassword
, final String Address) {
Userdatabase = FirebaseDatabase.getInstance().getReference("User").child(UserEmail);
Userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Log.i(TAG, "Username : " + Username + " Had Already Exist");
Toast.makeText(getApplicationContext(), "Username Exist !", Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
return;
}
});
firebaseAuth.createUserWithEmailAndPassword(UserEmail,Password).addOnCompleteListener(new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (!task.isSuccessful()) {
Log.i(TAG, "Buyer FirebaseAuth Register : Fail");
Toast.makeText(getApplicationContext(), "Could Not Register Your Account", Toast.LENGTH_SHORT).show();
return;
}
else
{
Log.i(TAG, "Buyer FirebaseAuth Register : Success");
Userdatabase = FirebaseDatabase.getInstance().getReference("User").child(UserEmail);
final User user = new User(UserEmail, Password, Username, PhoneNumber, Address, ComfirmPassword);
Userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
Userdatabase.setValue(user);
Log.i(TAG, "FirebaseDatabase Add Buyer : Success");
Toast.makeText(getApplicationContext(), "Register Complete", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "Database Error");
}
});
}
}});
}
Firebase真的不允许我们将电子邮件地址存储为主键吗?
答案 0 :(得分:1)
否,您不能在键值中使用特殊字符,因为这会破坏Firebase的功能(尤其是自动构建的REST API)。
如果您的树的键中包含一个/
节点,那么尝试通过REST API进行访问将是不明确的:
http://yourproject.firebase.com/customers/mike/123
这是否意味着来自Mike的财产123
?还是以mike/123
作为键的节点?不可能知道...
您应该依靠客户节点的自动生成的ID,然后将电子邮件地址存储在此节点的字段中:
users
- h0r6TNCkEjMBGnYauwOdXhWt6oD3
- email:mike@example.com