如何阅读Firestore文档?

时间:2019-04-21 06:24:33

标签: java android google-cloud-firestore

我是Android开发的新手...;-)

我需要知道如何读取保存到Firestore的特定文档,而不必从Cloud Firestore控制台手动复制“ documentPath”!

您如何自动执行此操作?

接下来,我将一些代码配置为我需要配置的 documentPath

DocumentReference user = mFirestore.collection("Users").document(idUsers).collection("Companies").document(**"documentPath"**)

链接到图像:

Company that I registered now and that I wish to need to show the user automatically

链接到文档:

Document fields

我正在测试以下课程:

private void ReadSingleEmpresa() {

        FirebaseAuth autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
        String idUsuario = Base64Custom.codificarBase64(autenticacao.getCurrentUser().getEmail());

        DocumentReference user = mFirestore.collection("Users").document(idUsuario).collection("Companies").document("gaSpr59pbeMmO9UpFxQQ");//document path

        user.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {

                    DocumentSnapshot document = task.getResult();

                    if (document.exists()) {
                        Log.d("ler doc", "DocumentSnapshot data: " + document.getData());

                        StringBuilder fields = new StringBuilder("");

                        //Some document fields
                        fields.append("Company name: ").append(document.get("nomeEmpresa"));
                        fields.append("\nEmail: ").append(document.get("emailRepresentante"));
                        fields.append("\nTelephone number: ").append(document.get("telefoneRepresentante"));

                        txtEmpresa.setText(fields.toString());


                    } else {
                        Log.d("ler doc", "No such document");
                    }
                } else {
                    Log.d("ler doc", "get failed with ", task.getException());
                }
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                    }
                });
    }

这是结果:

Result of reading some document fields

它可以工作,但是我不知道如何在几个文件中获取刚刚注册的文件。我只有在手动复制文档ID时才得到它...

0 个答案:

没有答案