Bird
0
0

You wrote this Firestore code to get a document but docSnap.exists is false:

medium📝 Debug Q14 of 15
GCP - Cloud Firestore and Bigtable
You wrote this Firestore code to get a document but docSnap.exists is false:
const docRef = firestore.collection('users').doc();
const docSnap = await docRef.get();
What is the problem?
AMissing document ID in doc() causes a reference to a non-existing document.
BYou cannot call get() on a document reference.
CCollection name 'users' is invalid without quotes.
DYou must use firestore.doc() instead of collection().doc().
Step-by-Step Solution
Solution:
  1. Step 1: Check doc() usage

    Calling doc() without an ID creates a reference to a new document with an auto-generated ID, but it does not exist yet.
  2. Step 2: Understand get() on new doc reference

    Trying to get() returns a snapshot with exists=false because the document does not exist.
  3. Final Answer:

    Missing document ID in doc() causes a reference to a non-existing document. -> Option A
  4. Quick Check:

    doc() needs ID to get existing document [OK]
Quick Trick: Always provide document ID in doc() to read existing document [OK]
Common Mistakes:
  • Assuming doc() without ID points to existing document
  • Thinking get() cannot be called on document references
  • Confusing collection and document method usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GCP Quizzes