How to Read Data from Realtime Database ?



Follow the steps to read data from Firebase Realtime Database for Web :

There are 2 method to read data

  1. Using get( ) :

    get return a 'Promise' so to handle it we need to make 'async function'. 

import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { ref, get } from 'firebase/database';

async function readData() {
  const snapshot = await get(ref(database, 'REFERENCE_PATH'));
  const data = await snapshot.val();
  // data containe the value that is Read from Database
}
readData();

One more way to tackle with 'async function' using '.then( )'
get(ref(database, 'REFERENCE_PATH'))
  .then((snapshot) => {
    //'snapshot.exists' checks data is available or its null
    // And return boolean value
    if (snapshot.exists) {
      data = snapshot.val();
    }
  })
  .catch((error) => console.log(error));

2. Using onValue( )

It make a connection to database and calls automatically every time when data is changed at REFERENCE_PATH and get updated value. (It helps when you are making app like chat app.)
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { onValue, ref } from 'firebase/database';

onValue(ref(database, 'REFERENCE_PATH'), (snapshot) => (data = snapshot.val()));

You can also get data only once by adding object like this,
import { database } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { onValue, ref } from 'firebase/database';

onValue(
  ref(database, 'REFERENCE_PATH'),
  (snapshot) => (data = snapshot.val()),
  { onlyOnce: true }
);

If you using React then you can store snapshot.val() using 'useState' hook or make 'let' variable to get updated data.
let data;

Use this line above onValue() and get updated values in data variable.

Related Post
Read data
How to Read Data from Realtime Database ?
How to Read Data from Realtime Database ?

1.Setup and Initialize it, 2.You can read data using 'get()' and 'onValue()' method

21 October 2024

Setup and Initialize
Setup and Initialize Realtime Database
Setup and Initialize Realtime Database

1.Open realtime database dashboard, 2.Click Create Database, 3.Select location and Security rules, 4.Setup Firebase SDK and Initialize it

21 October 2024

Update and Delete data
How to Update and Delete data to Realtime Database ?
How to Update and Delete data to Realtime Database ?

1. Setup Firebase SDK and Initialize it, 2.Update data using 'update()' method 3.Delete data using 'remove()'method

21 October 2024

Write data
How to Save Data to Realtime Database ?
How to Save Data to Realtime Database ?

1. Setup Firebase SDK and Initialize it 2. Save data : use "set (ref(database, 'PATH_TO_SAVE'), 'DATA')" method

21 October 2024

Company
Explore

Copyright © 2024, All rights reserved by Firebase Master

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Allow", you consent to our use of cookies. Here is our Privacy Policy.