简体   繁体   English

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

[英]How do i retreive a document in firebase firestore android to match my particular login credentials

I have a firebase of this structure below我在下面有一个这种结构的火力基地

Structure of my Firestore Database我的 Firestore 数据库的结构

It has two users namely a church admin and normal church user它有两个用户,即教会管理员和普通教会用户

For Church Admin this is the code for Registering New Church对于教会管理员,这是注册新教会的代码

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();
    }
}

For User Registration I have对于用户注册,我有

 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();
    }
}

When succesfully registered i want a normal user to be able to login after authentication using any of the providers such as Facebook, Twitter or google, his portal should retreive his personalized portal using a combination of uuid and phone number成功注册后,我希望普通用户能够在使用 Facebook、Twitter 或 google 等任何提供商进行身份验证后登录,他的门户应该使用 uuid 和电话号码的组合检索他的个性化门户

for retrieve Church profile see the code below要检索教会资料,请参阅下面的代码

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();
    }
}

My question is how do i retrieve user details knowing fully well that to get the user data u have to follow the structure of我的问题是我如何完全了解要获取用户数据必须遵循以下结构来检索用户详细信息

rhema_churches = > church_name(needs to identify how to get this) = > document => members_data => user_data rhema_churches => Church_name(需要确定如何获得这个) => document => members_data => user_data

Please refer to database structure attached above请参考上面附上的数据库结构

Please kindly help me..请帮助我..

See what i tried anyway看看我尝试了什么

 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));

                    }
                }

            }
        });

    }
}

This method above needs two parameters to carry out its query namely uid and churchName上面的这个方法需要两个参数来执行它的查询,即uid和churchName

This is the method i tried for retreiving churchName but its not working这是我尝试检索教堂名称的方法,但它不起作用

  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 "";
}

Thanks !谢谢 !

So I was able to resolve my issues and my code is running fine..All thanks to Alex Mamo所以我能够解决我的问题并且我的代码运行良好..感谢 Alex Mamo

The first thing i had to do was to restructure my onboarding code for both church and user我必须做的第一件事是为教会和用户重新构建我的入职代码

For Church i have对于教会我有

  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();
    }
}

For User Onboarding I have对于用户入职,我有

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();
    }
}

So instead of having a collection inside another collection.. I simply had a single collection as rhema_churches and then had an ID for every type of user..因此,而不是在另一个集合中包含一个集合。我只是将一个集合作为 rhema_churches,然后为每种类型的用户设置了一个 ID。

See the structure below看下面的结构

New DB Structure新的数据库结构

With this structure it becomes difficult to login with more than one provider as each Provider you use is logged as the document id which is easy for retrieval使用这种结构,登录多个提供程序变得很困难,因为您使用的每个提供程序都被记录为易于检索的文档 ID

This is how the new query for User and Church now looks like这就是 User 和 Church 的新查询现在的样子

For Church Query we have对于教堂查询,我们有

 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();
    }


}

And then for user query I have然后对于用户查询我有

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));

                    }
                }

            }
        });

    }
}

The key take-away is never complicate your database structure too early..for any type of data that goes to my Firestore it has a usertype which can also help me in my filter关键是永远不会过早地使您的数据库结构复杂化......对于进入我的 Firestore 的任何类型的数据,它都有一个用户类型,它也可以帮助我进行过滤器

Thanks !谢谢 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 Firebase Firestore 文档中找到字段数。如何在 Android Studio 中执行此操作 - Is it possible to find the number of fields in Firebase Firestore document.How can i do it in Android Studio 如何将多个数据添加到 Android Firestore 中的一个文档? - How do I add multiple data to one document in Android Firestore? 如何从firebase中检索值并将值添加到其中并在android studio JAVA中更新到firebase - How can I retreive value from firebase and add value into it and update to firebase in android studio JAVA 如何在 Java 中将快照侦听器正确添加到 Firebase Firestore 文档参考中? - How do I correctly add a snapshot listener to a Firebase Firestore document reference in Java? 如何将多个图像的 downloadUrl 存储到 Firebase Firestore ? 无法在 Firestore 文档中的多个字段中创建和存储 - How do I store downloadUrl of multiple images to the Firebase Firestore ? Unable to create and store inside multiple fields inside Firestore document Firebase Firestore:如何在Android上将文档对象转换为POJO - Firebase Firestore : How to convert document object to a POJO on Android 我如何在我的 Android 应用程序中加入 Firestore - How I can do join with Firestore in my Android application 如何从Firebase中检索图像并显示在GridView Android中 - How can you retreive images from firebase and display in gridview android 如果我的所有文档 ID 都是自动生成的,如何在 Firestore 中查询用户信息? - How do I query a user's info in Firestore if all my document IDs are auto-generated? 如何在firestore的整个文档中搜索特定单词? - How to search for a particular word in whole document in firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM