The callback receives a snapshot of the current data at ref. Calling snapshot.val() returns the data object.
Step 2: Check the database value
The database value at ref is { name: 'Alice' }, so snapshot.val() returns this object.
Final Answer:
{ name: 'Alice' } -> Option D
Quick Check:
snapshot.val() returns current data [OK]
Hint: Remember: snapshot.val() gives current data [OK]
Common Mistakes:
Expecting snapshot itself to be data
Confusing null with empty data
Assuming an error if data exists
4. Consider this code snippet:
const unsubscribe = onValue(ref, snapshot => {
console.log(snapshot.val());
});
// Later in the code
unsubscribe();
What is the purpose of calling unsubscribe() here?
medium
A. To fetch data one more time
B. To stop listening for real-time updates
C. To reset the database reference
D. To delete the data at ref
Solution
Step 1: Understand the return value of onValue
onValue returns a function that, when called, stops the listener from receiving further updates.
Step 2: Analyze the effect of calling unsubscribe()
Calling unsubscribe() removes the listener, so no more console logs happen on data changes.
Final Answer:
To stop listening for real-time updates -> Option B
Quick Check:
Calling unsubscribe stops listener [OK]
Hint: Call unsubscribe to stop real-time listeners [OK]
Common Mistakes:
Thinking unsubscribe fetches data
Confusing unsubscribe with data deletion
Assuming unsubscribe resets reference
5. You want to read user profile data once and then listen for any updates to the user's status in real-time. Which code snippet correctly implements this using Firebase?