📖 Scenario: You are building a simple app to manage a list of tasks. You want to store tasks in Firebase Realtime Database. Each task has a title and a status.
🎯 Goal: Learn how to write data to Firebase Realtime Database using set, update, and push methods.
📋 What You'll Learn
Create an initial task object with exact fields
Create a reference path string for the tasks node
Use set to write a task to a specific path
Use update to change the status of a task
Use push to add a new task with a unique key
💡 Why This Matters
🌍 Real World
Writing and updating data in Firebase Realtime Database is common for apps like to-do lists, chat apps, and real-time dashboards.
💼 Career
Understanding how to write data using set, update, and push is essential for junior cloud developers and mobile app developers working with Firebase.
Progress0 / 4 steps
1
Create initial task data
Create a constant called task with an object containing title set to 'Buy groceries' and status set to 'pending'.
Create a constant called tasksRef and set it to the string 'tasks' representing the database path for tasks.
Firebase
Hint
Use const tasksRef = 'tasks';
3
Write task data using set
Write the task object to the database path tasksRef + '/task1' using the set method from Firebase. Use set(ref(database, path), data) syntax.
Firebase
Hint
Use set(ref(database, tasksRef + '/task1'), task); to write data.
4
Update and push tasks
Use update to change the status of task1 to 'completed' at path tasksRef + '/task1'. Then use push to add a new task with title 'Walk the dog' and status 'pending' under tasksRef.
Firebase
Hint
Use update(ref(database, tasksRef + '/task1'), { status: 'completed' }) and push(ref(database, tasksRef), newTask).
Practice
(1/5)
1. Which Firebase method would you use to completely replace all data at a specific database path?
easy
A. push()
B. update()
C. set()
D. get()
Solution
Step 1: Understand set() behavior
The set() method writes data to a specified path and replaces any existing data there.
Step 2: Compare with other methods
update() modifies specific fields without replacing all data, push() adds new child nodes, and get() reads data.
Final Answer:
set() -> Option C
Quick Check:
Replace all data = set() [OK]
Hint: Use set() to overwrite all data at a path [OK]
Common Mistakes:
Confusing update() with set() and expecting full replacement
Using push() when needing to replace data
Thinking get() writes data
2. Which of the following is the correct syntax to add a new child with a unique key under the path 'messages' using Firebase Realtime Database?
easy
A. firebase.database().ref('messages').set({ text: 'Hello' })
B. firebase.database().ref('messages').push({ text: 'Hello' })
C. firebase.database().ref('messages').update({ text: 'Hello' })
D. firebase.database().ref('messages').get({ text: 'Hello' })
Solution
Step 1: Identify method for unique child addition
The push() method adds a new child with a unique key under the specified path.
Step 2: Check syntax correctness
Using push({ text: 'Hello' }) under 'messages' correctly adds a new message with unique key.
Final Answer:
firebase.database().ref('messages').push({ text: 'Hello' }) -> Option B
Quick Check:
Unique child addition = push() [OK]
Hint: Use push() to add unique child nodes [OK]
Common Mistakes:
Using set() which overwrites all children
Using update() which only modifies existing fields
4. You want to update the user's email without removing other user data. Which Firebase method usage below contains an error?
medium
A. firebase.database().ref('users/user1').update({ email: 'new@example.com' })
B. firebase.database().ref('users/user1/email').set('new@example.com')
C. firebase.database().ref('users/user1').update({ email: 'new@example.com', phone: '1234567890' })
D. firebase.database().ref('users/user1').push({ email: 'new@example.com' })
Solution
Step 1: Understand push() usage
push() adds a new child with a unique key; it does not update existing fields.
Step 2: Analyze the goal
To update email without removing other data, update() is correct; push() wrongly adds a new child instead.
Final Answer:
Using push() to update email is incorrect -> Option D
Quick Check:
push() adds new child, not update fields [OK]
Hint: Use update() to change fields, not push() [OK]
Common Mistakes:
Using push() to update existing fields
Using set() which overwrites all data
Confusing update() and push() roles
5. You want to add multiple chat messages under 'chats' in Firebase Realtime Database, each with a unique key, and also update the user's last active timestamp without overwriting existing data. Which combination of methods should you use?
hard
A. Use push() for messages and update() for last active timestamp
B. Use push() for messages and set() for last active timestamp
C. Use update() for messages and set() for last active timestamp
D. Use set() for messages and update() for last active timestamp
Solution
Step 1: Add multiple messages with unique keys
Use push() under 'chats' to add each message with a unique key without overwriting others.
Step 2: Update last active timestamp without overwriting
Use update() on the user's data to change the timestamp without removing other fields.
Final Answer:
Use push() for messages and update() for last active timestamp -> Option A