Bird
Raised Fist0
Firebasecloud~10 mins

User profile data in Firebase - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - User profile data
User signs up or logs in
↓
Check if profile exists in Firestore
↓
Load profile
↓
User views or edits profile
↓
Save changes to Firestore
↓
Profile updated
This flow shows how user profile data is checked, created if missing, loaded, edited, and saved in Firebase Firestore.
Execution Sample
Firebase
const userId = auth.currentUser.uid;
const profileRef = firestore.collection('profiles').doc(userId);
const doc = await profileRef.get();
if (!doc.exists) {
  await profileRef.set({name: '', email: ''});
}
This code checks if a user profile exists in Firestore; if not, it creates a new empty profile.
Process Table
StepActionConditionResultFirestore State
1Get current user IDN/AuserId = 'abc123'No change
2Reference profile documentN/AprofileRef points to 'profiles/abc123'No change
3Get profile documentN/Adoc fetchedNo change
4Check if doc existsdoc.exists == falseProfile does not existNo change
5Create new profileN/ASet {name:'', email:''} in FirestoreNew profile created at 'profiles/abc123'
6EndN/AProfile ready for useProfile document exists
💡 Profile created because it did not exist before
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
userIdundefined'abc123''abc123''abc123''abc123'
doc.existsundefinedundefinedfalsefalsefalse
profileRefundefinedundefinedref to 'profiles/abc123'ref to 'profiles/abc123'ref to 'profiles/abc123'
Key Moments - 2 Insights
Why do we check if the profile document exists before creating it?
Because creating a profile without checking could overwrite existing data. See execution_table step 4 where doc.exists is false, so we create the profile only then.
What happens if the profile already exists?
The code skips creating a new profile and uses the existing data. This is implied by the condition in step 4; if doc.exists was true, creation would be skipped.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of doc.exists?
Atrue
Bundefined
Cfalse
Dnull
💡 Hint
Check the 'Condition' column in step 4 of execution_table
At which step is the new profile created in Firestore?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table
If the profile document already existed, which step would be skipped?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Refer to the condition in step 4 and what happens next in execution_table
Concept Snapshot
User profile data in Firebase Firestore:
- Check if profile document exists using get()
- If not exists, create with set()
- Use user ID as document key
- Load and update profile data as needed
- Always check existence to avoid overwriting
Full Transcript
This visual execution shows how Firebase Firestore handles user profile data. First, the app gets the current user's ID. Then it references the profile document in the 'profiles' collection using that ID. It fetches the document to check if it exists. If the profile does not exist, the app creates a new profile document with empty fields. This prevents overwriting existing profiles. Finally, the profile is ready for use or editing. Variables like userId and doc.exists change as the code runs. Key moments include why existence is checked and what happens if the profile exists. The quiz tests understanding of these steps.

Practice

(1/5)
1. What is the main purpose of storing user profile data in Firebase Firestore?
easy
A. To host static website files
B. To run server-side scripts automatically
C. To send emails to users
D. To save personal information for each user securely

Solution

  1. Step 1: Understand user profile data purpose

    User profile data holds personal details like name and email for each user.
  2. Step 2: Identify Firebase Firestore role

    Firestore stores this data securely and allows easy access and updates.
  3. Final Answer:

    To save personal information for each user securely -> Option D
  4. Quick Check:

    User profile data = personal info storage [OK]
Hint: User profiles store personal info, not run scripts or host files [OK]
Common Mistakes:
  • Confusing Firestore with hosting or email services
  • Thinking Firestore runs backend code automatically
  • Assuming user profiles are for sending emails
2. Which Firebase Firestore syntax correctly creates a document for a user profile with ID 'user123'?
easy
A. db.collection('users').doc('user123').set({name: 'Alice'})
B. db.users('user123').add({name: 'Alice'})
C. db.collection('users').add('user123', {name: 'Alice'})
D. db.doc('users/user123').create({name: 'Alice'})

Solution

  1. Step 1: Review Firestore document creation syntax

    Use collection('name').doc('id').set(data) to create or overwrite a document.
  2. Step 2: Match syntax with options

    db.collection('users').doc('user123').set({name: 'Alice'}) matches correct syntax: collection, doc, then set with data object.
  3. Final Answer:

    db.collection('users').doc('user123').set({name: 'Alice'}) -> Option A
  4. Quick Check:

    Correct Firestore doc creation = db.collection('users').doc('user123').set({name: 'Alice'}) [OK]
Hint: Use collection().doc().set() to create user profile docs [OK]
Common Mistakes:
  • Using add() with document ID specified
  • Wrong method names like create() instead of set()
  • Incorrect chaining of methods
3. Given this code snippet, what will be the output if the user document does not exist?
const docRef = db.collection('users').doc('user999');
docRef.get().then(doc => {
  if (doc.exists) {
    console.log(doc.data().name);
  } else {
    console.log('No user found');
  }
});
medium
A. No user found
B. undefined
C. Error: Document not found
D. null

Solution

  1. Step 1: Understand doc.exists property

    doc.exists is false if document does not exist in Firestore.
  2. Step 2: Follow code logic for missing document

    If doc.exists is false, code logs 'No user found'.
  3. Final Answer:

    No user found -> Option A
  4. Quick Check:

    Missing doc triggers else block = 'No user found' [OK]
Hint: Check doc.exists before accessing data to avoid errors [OK]
Common Mistakes:
  • Assuming doc.data() returns null or undefined
  • Expecting an error instead of else block output
  • Ignoring the else condition
4. Identify the error in this Firebase Firestore code to update a user's profile name:
db.collection('users').doc('user123').update({
  username: 'Bob'
});
medium
A. update() cannot be used to change existing fields
B. Missing await or then to handle promise
C. Field name should be 'name' not 'username'
D. Document ID 'user123' must be a number

Solution

  1. Step 1: Check Firestore update method usage

    update() returns a promise and should be handled with await or then.
  2. Step 2: Identify missing promise handling

    Code does not handle promise, which can cause silent failures.
  3. Final Answer:

    Missing await or then to handle promise -> Option B
  4. Quick Check:

    Firestore update returns promise, must handle it [OK]
Hint: Always handle promises with await or then in Firestore calls [OK]
Common Mistakes:
  • Thinking update() cannot change fields
  • Assuming field names must be fixed
  • Believing document IDs must be numeric
5. You want to store user profiles with optional phone numbers. Which Firestore data structure best handles missing phone numbers without errors?
hard
A. Set phone number field to empty string if missing
B. Always set phone number field to null
C. Include phone number field only if user provides it
D. Create a separate collection for phone numbers

Solution

  1. Step 1: Understand optional fields in Firestore

    Firestore allows documents to omit fields that are not provided.
  2. Step 2: Choose best practice for optional data

    Including the phone number field only when available avoids storing empty or null values and reduces confusion.
  3. Final Answer:

    Include phone number field only if user provides it -> Option C
  4. Quick Check:

    Omit optional fields if missing to keep data clean [OK]
Hint: Omit optional fields instead of storing empty or null values [OK]
Common Mistakes:
  • Storing empty strings which can confuse queries
  • Using null values that may cause unexpected behavior
  • Overcomplicating with separate collections for simple optional data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard