Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Document references must have an even number of segments

Error: Document references must have an even number of segments, but Users has 1

I have been looking through different posts on here and on different forums but all have the problem when first loading but my problem is after I logout or reset the password. When I load the contents from firebase I get the information but when I click on the sign out then go to login again it crash’s and I get this error. I have logged the users.uid and Document references and does not change after logging out.

My collection path is done with Constants so I don’t have a mis type.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have found that the error is in the Fragment side of my app in the FirestoreClass().loadUserData_fragment(this)
As commenting this line out after the log out will allow the app to run but in the activity the data can still be loaded as the activity load data and the fragment is the same so I don’t get why it wouldn’t load into the fragment after the sign out but will load first time.

Fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        FirestoreClass().loadUserData_fragment(this)
}

Activity

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityUpdateProfileBinding.inflate(layoutInflater)
        val view : LinearLayout = binding.root
        setContentView(view)

        setupActionBar()

        FirestoreClass().loadUserData(this)
}

GetCurrentUserID

 fun getCurrentUserID():String{
        // auto login

        var currentUser =  FirebaseAuth.getInstance().currentUser
        var currentUserId = ""
        if (currentUser != null){
            currentUserId = currentUser.uid
            Log.i("uis",currentUser.uid)
        }

        return  currentUserId
    }

Activity version

fun loadUserData(activity:Activity){
        mFireStore.collection(Constants.USERS)
            .document(getCurrentUserID())
            .get()
            .addOnSuccessListener { document ->
                val loggedInUser = document.toObject(User::class.java)!!
                Log.i("uis",getCurrentUserID() + Constants.USERS)
                when(activity){
                    is UpdateProfileActivity ->{
                        activity.setUserDataInUI(loggedInUser)
                    }
                    is LoginActivity -> {
                        // Call a function of base activity for transferring the result to it.
                        activity.userLoggedInSuccess(loggedInUser)
                    }


                }

            }
    }

Fragment version

 fun loadUserData_fragment(fragment: Fragment){
        mFireStore.collection(Constants.USERS)
            .document(getCurrentUserID())
            .get()
            .addOnSuccessListener { document ->
                val loggedInUser = document.toObject(User::class.java)!!
                Log.i("uis",getCurrentUserID() + Constants.USERS)
                when(fragment){
                    is HomeFragment ->{
                        fragment.setUserDataInUIFragment(loggedInUser)
                    }
                }

            }
    }

>Solution :

It seems that your getCurrentUserID() returns no value, which you’re not handling in your code. The best option is to only call loadUserData when there is an active user, but alternatively you can also check whether getCurrentUserID() returns a value:

fun loadUserData(activity:Activity){
  if (getCurrentUserID() != "") { // 👈
    mFireStore.collection(Constants.USERS)
        .document(getCurrentUserID())
        .get()
        .addOnSuccessListener { document ->
            ...
        }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading