我在firebase中有一个Android应用程序,我管理用户。我想检查,当我在应用程序中注册用户时,在我注册之前用户是否存在于firebase实时数据库中。我怎么能这样做?
db中的数据结构如下:
"users": {
"some_user_id": {
"name": "some_name"
"username: "aaaa"
},
"some_other_user_id": {
"name": "some_other_name"
"username: "aaaa"
}
}
答案 0 :(得分:0)
设置您的firebase,例如:
database.child("users")
.addValueEventListener(this);
并检查 onDataChange 函数中的结果:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean exists = false;
for (DataSnapshot child : dataSnapshot.getChildren()) {
Map<String, Object> model = (Map<String, Object>) child.getValue();
if(model.get("username").equals("SET_YOUR_NEW_USER_NAME_HERE")) {
exists = true;
break;
}
}
if(exists) {
// This user already exists in firebase.
}
else {
// This user doesn't exists in firebase.
}
}