android - 如何检索和显示登录用户的数据 - Firestore

标签 android firebase google-cloud-firestore

我试图在用户登录时获取并显示他们的信息。(即:姓名、电子邮件、电话)

我尝试了在 youtube 和堆栈溢出上找到的多个 fragment ,但都失败了。大多数教程使用实时数据库,这不是我想要的。

我也试过制作一个“用户”对象。

private void getData(){
        FirebaseFirestore db = FirebaseFirestore.getInstance();

        db.collection("users")
                //.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .whereEqualTo("email:", FirebaseAuth.getInstance().getCurrentUser().getUid())
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (DocumentSnapshot document : task.getResult()) {
                                //Toast.makeText(getApplicationContext(),document.getId() +"==>" + document.getData(),Toast.LENGTH_LONG).show();
                                //Toast.makeText(getApplicationContext(),""+ document.get("Email") ,Toast.LENGTH_LONG).show();

                                nameEdt.setText((CharSequence) document.get("First Name"));
                                emailEdt.setText((CharSequence) document.get("Email"));
                                phoneEdt.setText((CharSequence) document.get("Phone"));


                            }
                        } else {
                            Toast.makeText(getApplicationContext(),"No such document",Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

数据库结构: db structure

我知道 firestore 中的文档与用户没有关联,但我不知道如何设置我的代码以便它仅从已登录的用户检索数据* 它适用于新创建的帐户,但如果我要注销并使用其他用户登录,它不会更新“帐户/用户信息”。

简而言之,我将如何从登录用户访问和显示我的数据库信息?

补充说明:我正在使用电子邮件和密码进行身份验证

最佳答案

要访问存储在 Firestore 中的用户数据,它应该没有你想象的那么复杂,不需要查询,你只需要获取与用户的 uid 对应的文档,并获取特定字段或做任何你想做的事需要他们,像这样:

db.collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
        .get().addOnCompleteListener(task -> {
    if(task.isSuccessful() && task.getResult() != null){
        String firstName = task.getResult().getString("First Name");
        String email = task.getResult().getString("Email");
        String phone = task.getResult().getString("Phone");
        //other stuff
    }else{
        //deal with error
    }
});

原始答案:

用户信息不存储在Firestore数据库中,它们与您为登录设置的Firebase身份验证相关联。要检索相关用户信息,您需要使用相关的FirebaseAuth蜜蜂。使用它来检索当前登录用户:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

然后你可以用这样的东西得到名字和电子邮件:

String name = user.getDisplayName();
String email = user.getEmail();

有关详细信息,请参阅 documentation .


如果 FirebaseAuth 没有解析,这可能意味着您没有正确地遵循设置指南并且忘记在您的 gradle 文件中包含依赖项:

implementation 'com.google.firebase:firebase-auth:17.0.0'

关于android - 如何检索和显示登录用户的数据 - Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56249121/

相关文章:

android - 在 Android 应用中使用 Google 公交信息

swift - 同一应用程序中的两个观察者相互干扰

firebase - 禁用AccountChooser进行Firebase身份验证

Firebase 云功能异常缓慢

android - 在 Android 上超时后强制登录

android - 想要通知计数的完美圆圈

javascript - 如何从 firestore 获取数据并在其中获取另一个数据(通过引用)

node.js - Firestore 偶尔会触发超时

android - 用线连接 map 上的点

Firebase - 可以创建多少个数据库?