Document references must have an even number of segments, but 8VMCfTZIAmOwRRb2JZsVT8K04o22 has 1

Issue

I am trying to add data to Firestore Database, when a user is created, this is where i am getting this error: firestoreDb.document(uidd).collection("users").add(users)

This is my code:

auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
            btnRegister.isEnabled  false
            if (task.isSuccessful) {

                val success  true
                Log.d(TAG, "createUserWithEmail:success")
                val user  auth.currentUser
                uidd  auth.currentUser!!.uid
                val users  User(
                    auth.currentUser?.uid.toString(),
                    age)

                Toast.makeText(this, uidd, Toast.LENGTH_SHORT).show()
                firestoreDb  FirebaseFirestore.getInstance()
                firestoreDb.document(uidd).collection("users").add(users)

             } else {
                val success  false
                Log.w(TAG, "createUserWithEmail:failure", task.exception)
                Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show()
            }
        }

Please help me solve this issue 🙂

Solution

Directly under the root of Firestore you find collections, not documents.

I think what you’re trying to do is create a document for the user uidd in the users collection under the root. That’d be:

firestoreDb  FirebaseFirestore.getInstance()
firestoreDb.collection("users").document(uidd).set(users)

Also see the documentation on setting a value to a document.

Answered By – Frank van Puffelen

Leave a Comment