简体   繁体   中英

How to get PromiseValue with Promise {<pending>} Firebase?

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

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)

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