Anmol
Anmol

Reputation: 8670

Why Firestore DocumentSnapshot.exist() is always `false` addOnCompleteListener?

I am creating basic application to retrieve user data from Firestore and if the user is new(in Firestore i.e. document should not exist) i am adding a welcome note(some default data to DB).

private fun createUserIfNeeded(user: FirebaseUser) {
    fireBaseFireStore.collection("Users").document(user.uid).get().addOnCompleteListener {
        if (it.isSuccessful && !it.result!!.exists()) {
            val defaultData = 
                hashMapOf("head" to "Hello", "body" to "Welcome to NoteIt...")

            fireBaseFireStore
                .collection("Users")
                .document(user.uid)
                .collection("Notes")
                .document("Default")
                .set(defaultData)
        }
    }
}

When I run above code document.result.exist is always false even when document exist.

i tried using addOnSuccessListener also same result.

Am i doing something wrong?

I referred many questions related to this but so solution helped.

P.S: new to Firebase and it's APIs

UPDATE: If i am trying to check for any of the inner collection's and document then the result's are working fine

private fun createUserIfNeeded(user: FirebaseUser) {
        val notesRef = fireBaseFireStore
            .collection("Users")
            .document(user.uid)
            .collection("Notes")

        notesRef
            .get()
            .addOnCompleteListener {
                if (it.result == null || it.result!!.documents.size == 0) {
                    val defaultData = hashMapOf("head" to "Hello", "body" to "Welcome to NoteIt...")
                    notesRef
                        .document("Default")
                        .set(defaultData)
                }
            }
    }

I am not able to understand what the problem is.

Upvotes: 1

Views: 534

Answers (1)

Erik
Erik

Reputation: 5119

try this, Uninstall the app and reinstall, sometimes the Firestore local cache version of you db need to be flushed, during dev

When developing one can by mistake view the wrong DB in Firestore console, lets say your app have a developer version and a production version, to little coffee and one might looking at wrong stuff

Upvotes: 2

Related Questions