[英]Firebase checking if exists returns false where it should return true
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.
使用此电子邮件的用户位于数据库中,因此检查应返回true而不是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). 如果
setPersistenceEnabled(true)
,则使用addListenerForSingleValueEvent
可以导致返回本地缓存版本的数据,而不是从服务器返回最新版本(而调用addValueEventListener
将确保获得最新版本)。 Another approach, if using addListenerForSingleValueEvent
is to call query.keepSynced(true);
另一种方法,如果使用
addListenerForSingleValueEvent
则调用query.keepSynced(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.