Search⌘ K
AI Features

Read, Write, and Delete Data from the Realtime Database

Explore how to interact with Firebase Realtime Database by creating references, performing write operations with set and push, reading data with get and onValue, and deleting data safely. This lesson helps you understand real-time data management and optimize database use in applications.

To carry out any operation on the Realtime Database, we must create a reference to the specific path within our database. To do this, we use the ref function, imported from the firebase/database subpackage. This function returns a reference to the location in the database that corresponds to the provided path.

The ref function takes the database instance as its first argument. It can also optionally take the path that the returned reference points to, as its second argument. However, if no path is provided, the function returns a reference to the root of the database:

Javascript (babel-node)
import { getDatabase, ref } from "firebase/database";
// initialise database
const db = getDatabase();
// reference to database root
const dbRef = ref(db);
// reference to the users/tasks/* path
const tasksRef = ref(db, "users/tasks/" + user.uid);

Basic write operations

We use ...