我如何在 firebase firestore android 中检索文档以匹配我的特定登录凭据

Posted

技术标签:

【中文标题】我如何在 firebase firestore android 中检索文档以匹配我的特定登录凭据【英文标题】:How do i retreive a document in firebase firestore android to match my particular login credentials 【发布时间】:2020-06-05 06:37:09 【问题描述】:

我在下面有一个这种结构的火力基地

Structure of my Firestore Database

它有两个用户,分别是教会管理员和普通教会用户

教会管理员这是注册新教会的代码

public void onBoardChurchDetails(String church_name, String church_addr, String church_email, String church_leadName, String churchPhone, String church_icon, String churchDescr, String church_regStat, final String uid) 
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        rhemaCollRef = rChurchFireStore.collection("rhema_churches");
        rhemaHiveChurchSubClass = getRhemaHiveChurchSubClass();

        if (rhemaHiveChurchSubClass.checkChurchEmptyParams(church_icon, church_name, church_addr, churchPhone, church_email, church_leadName, churchDescr, church_regStat,uid) == 1) 

            rhemaHiveChurchModelClass = getChurchModelClass(church_name, church_addr, church_email, church_leadName, churchPhone, church_icon, churchDescr, church_regStat,uid);

            rhemaCollRef.document(church_name).set(rhemaHiveChurchModelClass).addOnCompleteListener(new OnCompleteListener<Void>() 
                @Override
                public void onComplete(@NonNull Task<Void> task) 

                    if (task.isSuccessful()) 
                        try 
                            getAuto().getToast(c, " Congratulations you Just Registered a new Church", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                            intent  = new Intent(c, RhemaHiveChurchPortal.class);
                            bundle.putString("church_id", uid);

                            intent.putExtras(bundle);
                            startActivity(intent);
                            clearChurchDetails();
                        
                        catch(NullPointerException np)
                            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        
                     else 
                        getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + task.getException().getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        startActivity(getAuto().newActivityStarter(c, RhemaHiveChurchPortal.class));
                    
                
            ).addOnFailureListener(new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 
                    getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                
            );


         else 

            getAuto().getToast(c, "Oops Seems we are missing out some data here", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

        

     else 
        getAuto().getToast(c, "Oops Seems No Internet Service is connected", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

对于用户注册我有

 public void onBoardUserWithoutSocial(String fName, String lName, String gender, String userType, String dob, String regStat, String imgPath, String userEmail, String phone , String about, final String churchName, String branchName, String addr, String city, String country, String postalCode, final String uid) 
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        fstore = getFstore();
    fiReference = fstore.collection("rhema_churches");
    rhemaHiveUserSubClass = getRhemaHiveUserSubClass();

        if (rhemaHiveUserSubClass.checkEmptyParam(fName, lName, branchName, churchName, addr, country, city, dob, gender, postalCode, regStat, phone,  userType,imgPath,userEmail,about,uid) == 1) 
            rhemaHiveUserModelClass = getUserModelClass(fName,lName,gender,userType,dob,regStat, imgPath,userEmail,phone,about,churchName,branchName,addr,city,country,postalCode,uid);
            fiReference.document(churchName).collection("members_data").document(getFullName(fName,lName)).set(rhemaHiveUserModelClass).addOnCompleteListener(new OnCompleteListener<Void>() 
                @Override
                public void onComplete(@NonNull Task<Void> task) 
                    if(task.isSuccessful())
                        try
                        getAuto().getToast(c, "Congratulation You've onboarded sucessfully..",RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                            bundle.putString("user_id", uid);
                            bundle.putString("user_church",churchName);

                            intent.putExtras(bundle);
                        startActivity(intent);
                        clearUserDetails();
                        
                        catch(NullPointerException np)
                            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        

                    
                    else
                        getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + task.getException().getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

                    
                
            ).addOnFailureListener(new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 
                    getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                
            );

         else 
            getAuto().getToast(c, "Oops ! !..Seems we are missing out some of your details", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
        
    
    else
        getAuto().getToast(c, "Oops ! !..Seems you need to put on your data", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

成功注册后,我希望普通用户能够在使用任何提供商(例如 Facebook、Twitter 或 google)进行身份验证后登录,他的门户应该使用 uuid 和电话号码的组合来检索他的个性化门户

要检索教堂资料,请参见下面的代码

public void retreiveChurchDetails(String id) throws NullPointerException
    if(networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
    FirebaseFirestore fS = getFStore();
    rhemaCollRef = fS.collection("rhema_churches");
    rhemaCollRef.whereEqualTo("church_uid", id ).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                for(DocumentSnapshot documentSnapshot  : queryDocumentSnapshots.getDocuments())
                    church_name = documentSnapshot.getString("church_name");
                    church_email = documentSnapshot.getString("church_email");
                    church_phone = documentSnapshot.getString("church_phone");
                    church_pix_link = documentSnapshot.getString("church_pix");
                    getTextView(R.id.church_retr_name).setText(church_name);
                    getTextView(R.id.church_retr_email).setText(church_email);
                    getTextView(R.id.church_retr_phone).setText(church_phone);
                    ld_wt_gl(c,church_pix_link,44,44,getImg(R.id.church_retr_logg));

                
        
    ).addOnFailureListener(new OnFailureListener() 
        @Override
        public void onFailure(@NonNull Exception e) 
            getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
        
    );
    

    else
        getAuto().getToast(c, "Oops ! !..Seems we need an internet connection for this", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

我的问题是我如何充分了解要获取用户数据,您必须遵循以下结构来检索用户详细信息

rhema_churches = > Church_name(需要确定如何获取)=> 文档 => members_data => user_data

请参考上附的数据库结构

请帮帮我..

看看我尝试了什么

 public void retrUserDet(final String uid, String church_name) throws NullPointerException
    if(networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        FirebaseFirestore fS = getFStore();
        rhemaCollRef = fS.collection("rhema_churches");
        rhemaCollRef.document(church_name).collection("members_data").whereEqualTo("user_uid",uid).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                if(!queryDocumentSnapshots.getDocuments().isEmpty())
                    for(DocumentSnapshot doc : queryDocumentSnapshots.getDocuments())
                        user_email = doc.getString("user_email");
                        user_name = doc.getString("user_fName") + doc.getString("user_lName");
                        user_phone = doc.getString("user_phone");
                        user_pix_link = doc.getString("user_pix");
                        getTex(R.id.user_retr_email).setText(user_email);
                        getTex(R.id.user_retr_name).setText(user_name);
                        getTex(R.id.user_retr_phone).setText(user_phone);
                        ld_wt_gl(c,user_pix_link,44,44,getImg(R.id.user_retr_logg));

                    
                

            
        );

    

上面这个方法需要两个参数来进行查询,即uid和churchName

这是我尝试检索教堂名称的方法,但它不起作用

  public String retrChurchName(String uid)
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        rhemaCollRef = rChurchFireStore.collection("rhema_churches");
        //rhemaCollRef.
        rhemaCollRef.whereEqualTo("uid",uid).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                for(DocumentSnapshot documentSnapshot : queryDocumentSnapshots.getDocuments()
                )
                
                    if(documentSnapshot.exists())
                        churchName = documentSnapshot.getString("church_name");
                    

                    else
                        getAuto().getToast(c,"Document doesnt exist", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

                    
                
            
        ).addOnFailureListener(new OnFailureListener() 
            @Override
            public void onFailure(@NonNull Exception e) 
                getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
            
        );

        return churchName;

    

    else
        getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + networkInfo.getExtraInfo(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

    return "";

谢谢!

【问题讨论】:

这个answer 可能会有所帮助。 我理解你的实现,我应该有一个 UID 作为我的集合之前的文档,这很有意义并且便于查询..我会尝试一下,看看它是否有效..谢谢 【参考方案1】:

所以我能够解决我的问题并且我的代码运行良好..感谢 Alex Mamo

我要做的第一件事是为教会和用户重新构建我的入职代码

为了教会我有

  public void onBoardChurchDetails(String church_name, String church_addr, String church_email, String church_leadName, String churchPhone, String church_icon, String churchDescr, String church_regStat, final String uid, String userType) 
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        rhemaCollRef = rChurchFireStore.collection("rhema_churches");
        rhemaHiveChurchSubClass = getRhemaHiveChurchSubClass();

        if (rhemaHiveChurchSubClass.checkChurchEmptyParams(church_icon, church_name, church_addr, churchPhone, church_email, church_leadName, churchDescr, church_regStat, uid, userType) == 1) 

            rhemaHiveChurchModelClass = getChurchModelClass(church_name, church_addr, church_email, church_leadName, churchPhone, church_icon, churchDescr, church_regStat, uid, userType);

            rhemaCollRef.document(uid).set(rhemaHiveChurchModelClass).addOnCompleteListener(new OnCompleteListener<Void>() 
                @Override
                public void onComplete(@NonNull Task<Void> task) 

                    if (task.isSuccessful()) 
                        try 
                            getAuto().getToast(c, " Congratulations you Just Registered a new Church", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

                            bundle.putString("church_id", uid);

                            intent.putExtras(bundle);
                            startActivity(intent);
                            //clearChurchDetails();
                         catch (NullPointerException np) 
                            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        
                     else 
                        getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + task.getException().getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        startActivity(getAuto().newActivityStarter(c, RhemaHiveChurchPortal.class));
                    
                
            ).addOnFailureListener(new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 
                    getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                
            );


         else 

            getAuto().getToast(c, "Oops Seems we are missing out some data here", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

        

     else 
        getAuto().getToast(c, "Oops Seems No Internet Service is connected", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

对于用户入职我有

public void onBoardUserWithoutSocial(String fName, String lName, String gender, String userType, String dob, String regStat, String imgPath, String userEmail, String phone , String about, final String churchName, String branchName, String addr, String city, String country, String postalCode, final String uid) 
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        fstore = getFstore();
    fiReference = fstore.collection("rhema_churches");
    rhemaHiveUserSubClass = getRhemaHiveUserSubClass();

        if (rhemaHiveUserSubClass.checkEmptyParam(fName, lName, branchName, churchName, addr, country, city, dob, gender, postalCode, regStat, phone,  userType,imgPath,userEmail,about,uid) == 1) 
            rhemaHiveUserModelClass = getUserModelClass(fName,lName,gender,userType,dob,regStat, imgPath,userEmail,phone,about,churchName,branchName,addr,city,country,postalCode,uid);
            fiReference.document(uid).set(rhemaHiveUserModelClass).addOnCompleteListener(new OnCompleteListener<Void>() 
                @Override
                public void onComplete(@NonNull Task<Void> task) 
                    if(task.isSuccessful())
                        try
                        getAuto().getToast(c, "Congratulation You've onboarded sucessfully..",RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                            bundle.putString("user_id", uid);
                            bundle.putString("user_church",churchName);

                            intent.putExtras(bundle);
                        startActivity(intent);
                        clearUserDetails();
                        
                        catch(NullPointerException np)
                            getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + np.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                        

                    
                    else
                        getAuto().getToast(c,RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + task.getException().getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();

                    
                
            ).addOnFailureListener(new OnFailureListener() 
                @Override
                public void onFailure(@NonNull Exception e) 
                    getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(),RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
                
            );

         else 
            getAuto().getToast(c, "Oops ! !..Seems we are missing out some of your details", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
        

    




    else
        getAuto().getToast(c, "Oops ! !..Seems you need to put on your data", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    

因此,我没有在另一个集合中包含一个集合。我只是将一个集合作为 rhema_churches,然后为每种类型的用户都有一个 ID。

看下面的结构

New DB Structure

使用这种结构,使用多个提供者登录变得很困难,因为您使用的每个提供者都记录为易于检索的文档 ID

这就是用户和教堂的新查询现在的样子

对于教堂查询,我们有

 public void retreiveChurchDetails(String id) throws NullPointerException 
    if (networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        FirebaseFirestore fS = getFStore();
        rhemaCollRef = fS.collection("rhema_churches");
        rhemaCollRef.whereEqualTo("church_uid", id).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots.getDocuments()) 
                    church_name = documentSnapshot.getString("church_name");
                    church_email = documentSnapshot.getString("church_email");
                    church_phone = documentSnapshot.getString("church_phone");
                    church_pix_link = documentSnapshot.getString("church_pix");
                    getTextView(R.id.church_retr_name).setText(church_name);
                    getTextView(R.id.church_retr_email).setText(church_email);
                    getTextView(R.id.church_retr_phone).setText(church_phone);
                    ld_wt_gl(c, church_pix_link, 88, 88, getImg(R.id.church_retr_logg));

                
            
        ).addOnFailureListener(new OnFailureListener() 
            @Override
            public void onFailure(@NonNull Exception e) 
                getAuto().getToast(c, RhemaHiveClassReferenceConstants.ERROR_MESSAGE_GENERIC + e.getLocalizedMessage(), RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
            
        );
     else 
        getAuto().getToast(c, "Oops ! !..Seems we need an internet connection for this", RhemaHiveClassReferenceConstants.TOAST_SHORT_LEN).show();
    



然后对于用户查询我有

public void retrUserDet(final String uid) throws NullPointerException
    if(networkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isConnected()) 
        FirebaseFirestore fS = getFStore();
        rhemaCollRef = fS.collection("rhema_churches");
        rhemaCollRef.whereEqualTo("user_uid",uid).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                if(!queryDocumentSnapshots.getDocuments().isEmpty())
                    for(DocumentSnapshot doc : queryDocumentSnapshots.getDocuments())
                        user_email = doc.getString("user_email");
                        user_fName  = doc.getString("user_fName");
                        user_lName = doc.getString("user_lName");

                        user_phone = doc.getString("user_phone");
                        user_pix_link = doc.getString("user_pix");
                        getTex(R.id.user_retr_email).setText(user_email);
                        getTex(R.id.user_retr_name).setText(retFullName(user_fName,user_lName));
                        getTex(R.id.user_retr_phone).setText(user_phone);
                        ld_wt_gl(c,user_pix_link,120,120,getImg(R.id.user_retr_logg));

                    
                

            
        );

    

关键的一点是永远不要过早地使您的数据库结构复杂化..对于进入我的 Firestore 的任何类型的数据,它都有一个用户类型,它也可以帮助我进行过滤

谢谢!

【讨论】:

以上是关于我如何在 firebase firestore android 中检索文档以匹配我的特定登录凭据的主要内容,如果未能解决你的问题,请参考以下文章

如何将我的 Firebase 实时数据库转移到 Firebase Cloud Firestore

如何将图像上传到firebase存储然后将图像url存储在firestore中?

如何在 Firebase Firestore 中获取内部集合

Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中?

如何在 Flutter 中使用 Firebase Auth 和 Firestore

如何修复 React Native 中的 Firebase/firestore 错误?