Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to read data once from Firebase Realtime Database.
Firebase
firebase.database().ref('users').[1](snapshot => { console.log(snapshot.val()); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' causes continuous listening.
Using 'get' is not a method on the reference in this context.
✗ Incorrect
The once method reads data a single time from the database.
2fill in blank
mediumComplete the code to listen for real-time updates on the 'messages' node.
Firebase
firebase.database().ref('messages').[1]('value', snapshot => { console.log(snapshot.val()); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' reads data only once, not continuously.
Using 'get' or 'fetch' are not valid methods here.
✗ Incorrect
The on method sets up a listener for real-time updates.
3fill in blank
hardFix the error in the code to properly remove the listener.
Firebase
const ref = firebase.database().ref('tasks'); ref.on('value', snapshot => { console.log(snapshot.val()); }); // Remove listener ref.[1]('value');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'removeListener' or 'stop' causes errors because they don't exist.
Not removing listeners can cause memory leaks.
✗ Incorrect
The off method removes listeners previously attached with on.
4fill in blank
hardFill both blanks to read data once and handle errors.
Firebase
firebase.database().ref('profiles').[1]('value').then(snapshot => { console.log(snapshot.val()); }).[2](error => { console.error(error); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' reads data continuously.
Using 'then' instead of 'catch' for error handling is incorrect.
✗ Incorrect
Use once to read data once, and catch to handle errors in the promise.
5fill in blank
hardFill all three blanks to listen for child added events and log the new child's key and value.
Firebase
firebase.database().ref('notifications').[1]('child_added', snapshot => { const key = snapshot.[2]; const value = snapshot.[3](); console.log(key, value); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' instead of 'on' causes only one event.
Using 'val' without parentheses causes errors.
Accessing 'key' as a method instead of a property causes errors.
✗ Incorrect
Use on to listen continuously, key to get the child's key, and val() to get its value.