检查用户是否存在于Firestore中 [英] Check if user exists in Firestore
问题描述
我想在Cloud Firestore中创建一个用户数据库,并将电子邮件作为每个用户的唯一ID。用户应该在集合
中,每个用户都是文档
。
这是我如何检查用户是否存在于登录活动中:
FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference userRef = db.collection(users)。document(email);
userRef.get()。addOnCompleteListener(new OnCompleteListener< DocumentSnapshot>(){
@Override $ b $ public void onComplete(@NonNull Task< DocumentSnapshot> task){
if .isSuccessful()){
DocumentSnapshot document = task.getResult();
$ b toast.makeText(LoginActivity.this,task.getResult()。toString(),
Toast .LENGTH_SHORT).show();
if(document!= null){
//用户存在...
}
else {
//用户不存在...
}
else
connectionErrorActivity();
}
});
数据库为空,如您所见:
方法。 c> getData 来获取它的内容。
I'd like to create a users database in Cloud Firestore, with the email as the unique ID for each user. The users should be in a collection
, and each user is a document
.
This is how I check if the user exists in the login activity:
FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference userRef = db.collection("users").document(email);
userRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
Toast.makeText(LoginActivity.this, task.getResult().toString(),
Toast.LENGTH_SHORT).show();
if (document != null) {
//The user exists...
}
else {
//The user doesn't exist...
}
}
}
else
connectionErrorActivity();
}
});
The database is empty, as you can see:
My problem is, that document
is never null, even tough the database is empty. The toast
shows that the string value of document
is: COM.GOOGLE.FIREBASE.FIRESTORE.DOCUMENTSNAPSHOT@8ED6B96
.
Is this the correct way to do this? What can I do to fix it?
The result of this Task
is a DocumentSnapshot
. Whether or not the underlying document actually exists is available via the exists
method.
If the document does exist you can call getData
to get at the contents of it.
这篇关于检查用户是否存在于Firestore中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!