Zorgan
Zorgan

Reputation: 9123

db.collection().document().get() never returns null

MainActivity.kt

val db = FirebaseFirestore.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val user = db.collection("zzz").document("ttt")
    user.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                Log.d(TAG, "DocumentSnapshot: ${document}")
            }
        }

My Log prints DocumentSnapshot: DocumentSnapshot{key=zzz/ttt, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}.

No matter what collection() value or document() value I make user - it will never return null.

Why is this?

PS: The document also doesn't appear in my Firebase console.

Upvotes: 1

Views: 826

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

You should use exists() instead:

if (document.exists()) {
                Log.d(TAG, "DocumentSnapshot: ${document}")
            }

From the docs:

public boolean exists()

Returns true if the document existed in this snapshot

Upvotes: 2

Related Questions