Ilya
Ilya

Reputation: 13

How do I get data from firebase realtime database in web?

I created the app, installed CLI, added scripts to index.html. Then i added record {number: 1} in Realtime Database, logged in and initialized firebase in project folder.

Realtime Database

This is my index.html

<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js"></script>



<!-- TODO: Add SDKs for Firebase products that you want to use
       https://firebase.google.com/docs/web/setup#available-libraries -->
  <script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-database.js"></script>



<script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
      ***config***
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    const db = firebase.database();
    console.log(db);
  </script>

Why do I get such data? Why not {number: 1}?

const db = firebase.database()

Upvotes: 1

Views: 4201

Answers (1)

Praveen Patel
Praveen Patel

Reputation: 347

// Get a reference to the database service
var database = firebase.database();

// This is reference to tha database, to fetch data from database use below code:

var starCountRef = firebase.database().ref('posts');
// Add ref of child if any
starCountRef.on('value', function(snapshot) {
    console.log(snapshot.val());
});

Ref : https://firebase.google.com/docs/database/web/read-and-write

Use val() function get data in json format

Upvotes: 1

Related Questions