检查用户是否存在于 Firestore [英] Check if user exists in Firestore

查看:33
本文介绍了检查用户是否存在于 Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Cloud Firestore 中创建一个用户数据库,将电子邮件作为每个用户的唯一 ID.用户应该在一个collection中,每个用户就是一个document.

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:

我的问题是,document 永远不会为空,即使数据库是空的. toast 显示 的字符串值code>document 是:COM.GOOGLE.FIREBASE.FIRESTORE.DOCUMENTSNAPSHOT@8ED6B96.

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?

推荐答案

这个 Task 的结果是一个 DocumentSnapshot.通过 存在方法.

The result of this Task is a DocumentSnapshot. Whether or not the underlying document actually exists is available via the exists method.

如果文档确实存在,您可以调用 getData 来获取它的内容.

If the document does exist you can call getData to get at the contents of it.

这篇关于检查用户是否存在于 Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆