0

I'm trying to get data from the db, but it does not work How to get data from bd?

const fire = firebase.initializeApp(config);
const db = fire.database();
const thisDb = db.ref().child('rooms');
thisDb.on('value', (snap) => {
  console.log(snap);
});

console

enter image description here

data from db

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kovich
  • 61
  • 1
  • 7
  • Use this reference: https://firebase.google.com/docs/database/admin/retrieve-data – kiran malvi Aug 02 '18 at 09:24
  • why the variables that I assign inside thisDb.on ('value', (snap) => {}); not visible from the outside? `let arr = []; thisDb.on('value', (snap) => { arr = snap.val(); }); console.log(arr); // empty` – Kovich Aug 02 '18 at 11:12
  • The callback you pass into `on` is called asynchronously, once the data is loaded from Firebase. Since this may take some time, when you try to access the data *outside* of this callback, it likely hasn't loaded yet. The solution is to only use the data *inside* the callback, or to use a promise. See https://stackoverflow.com/a/26643970, https://stackoverflow.com/q/27474546, https://stackoverflow.com/a/48068397, https://stackoverflow.com/a/33807995 – Frank van Puffelen Aug 02 '18 at 13:49

0 Answers0