I'm trying to check if a user with a particular email exists or not. The user with this email is in the database so the check should have returned true instead of false.
Here's the schema:
Here's the code:
final DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("users");
userRef.orderByChild("email").equalTo("rafael.adel20@gmail.com").limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot); //returns { key = users, value = null }
if(dataSnapshot.getChildrenCount() > 0) {
//user already exists (Never executed)
} else {
//user doesn't exists (Always executed)
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
If setPersistenceEnabled(true)
is called, use of addListenerForSingleValueEvent
can cause local cached version of data to be returned rather than latest version from server (whereas calling addValueEventListener
will ensure you get latest version). Another approach, if using addListenerForSingleValueEvent
is to call query.keepSynced(true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.