我的Firebase数据库示例:-用户-> -jjdsHGkjldsgJSKD->-用户名:“ kp”,-电子邮件“ kp@kp.com”
这是要检查Firebase数据库中用户名是否退出的代码块
mRef.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()) {
if (data.child("username").exists()) {
Toast.makeText(RegisterActivity.this, "USERNAME IS ALREADY TAKEN", Toast.LENGTH_LONG).show();
Log.i("username", "Username used: " + data.child("username").getValue());
count++;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
答案 0 :(得分:0)
您将无法正常工作。这应该更接近:
String valueFromInputText = "kishkish96";
mRef.child("Users")
.orderByChild("username");
.equalTo(valueFromInputText)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(RegisterActivity.this, "USERNAME IS ALREADY TAKEN", Toast.LENGTH_LONG).show();
Log.i("username", "Username used: " + data.child("username").getValue());
count++;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore exceptions
}
});
更改: