Reading Data from Firebase Realtime Database
Reading Data Once
To read data once from Firebase Realtime Database, use the once method. This is useful for fetching data without listening for changes.
function readUserData(userId) {
const userRef = firebase.database().ref('users/' + userId);
userRef.once('value').then((snapshot) => {
const data = snapshot.val();
console.log(data);
});
}
// Example usage
readUserData('1');
Explanation: The `readUserData` function reads user data from the Firebase Realtime Database by retrieving the data once for the given `userId` from the path `users/userId` and logging it to the console. For example, calling `readUserData(β1β)` will fetch and log the data of the user with ID β1β.
Listening for Data Changes
To listen for real-time updates, use the on method. This will trigger every time the data at the specified reference changes.
function listenForUserData(userId) {
const userRef = firebase.database().ref('users/' + userId);
userRef.on('value', (snapshot) => {
const data = snapshot.val();
console.log(data);
});
}
// Example usage
listenForUserData('1');
Output
For the readUserData function, the console output will be:
{
"username": "John Doe",
"email": "john.doe@example.com"
}
Explanation: The `listenForUserData` function sets up a real-time listener on the Firebase Realtime Database to continuously monitor and log changes to the data of the user with the given `userId` from the path `users/userId`. For example, calling `listenForUserData(β1β)` will log the data of the user with ID β1β whenever it changes.
Updating Data
To update specific fields in our data without overwriting the entire node, use the update method:
function updateUserData(userId, email) {
const updates = {};
updates['/users/' + userId + '/email'] = email;
firebase.database().ref().update(updates);
}
// Example usage
updateUserData('1', 'new.email@example.com');
Output
After running the updateUserData function, the database will look like this:
{
"users": {
"1": {
"username": "John Doe",
"email": "new.email@example.com"
}
}
}
Deleting Data
To delete data from Firebase Realtime Database, use the remove method:
function deleteUser(userId) {
firebase.database().ref('users/' + userId).remove()
.then(() => {
console.log('User removed successfully.');
})
.catch((error) => {
console.error('Error removing user:', error);
});
}
// Example usage
deleteUser('1');
Output: After running the deleteUser function, the user data will be removed from the database.
Explanation: The `deleteUser` function removes the user data with the specified `userId` from the Firebase Realtime Database at the path `users/userId` and logs a success message or an error message if the removal fails. For example, calling `deleteUser(β1β)` will delete the data for the user with ID β1β.
Firebase Realtime Database: Reading and Writing Data
Firebase Realtime Database, a cloud-hosted NoSQL database developed by Google, provides a robust solution for achieving seamless real-time data updates across connected clients.
In this article, We will learn about the Firebase Realtime Database, How to Setting Up the Firebase Realtime Database, write data or read data to Firebase Realtime Database with an example of a Real-Time Simple Chat Application.