简体   繁体   中英

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.

实时数据库

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()

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM