0

Good afternoon. I'm new to working with Firebase batabase. Please tell me how to get data from my database to work with them farther. Now my function returns Promise {} with the date that I need inside PromiseValue. How do I get it right. Now my code looks like this

let firebaseConfig = {
....
};

firebase.initializeApp(firebaseConfig);

let ref = firebase.database().ref('/data')

function getDataPromise() {
    return ref.once('value').then(function(snapshot) {
      return snapshot.val();
    });
  }
let res =  getDataPromise()
console.log(res)

I will be glad to any answer

incognita
  • 45
  • 5

1 Answers1

1

You have to use then() to get the data,like below

let firebaseConfig = { 
  ....
};

firebase.initializeApp(firebaseConfig);

let ref = firebase.database().ref('/data')

function getDataPromise() {
  return ref.once('value').then(function(snapshot) {
    return snapshot.val();
  });
}

let res =  getDataPromise()
// Use then() with a callback to get data
res.then(data => {
  console.log(data)
})
console.log(res)
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46