gpt4 book ai didi

javascript - 如何使用 Promise {} Firebase 获取 PromiseValue?

转载 作者:行者123 更新时间:2023-12-02 21:55:50 26 4
gpt4 key购买 nike

下午好。我是 Firebase batabase 的新手。请告诉我如何从我的数据库中获取数据以便进一步与他们合作。现在我的函数返回 Promise {} 以及 PromiseValue 中我需要的日期。我怎样才能做到正确。现在我的代码如下所示

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)

我很高兴得到任何答复

最佳答案

您必须使用then()获取数据,如下所示

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)

关于javascript - 如何使用 Promise {<pending>} Firebase 获取 PromiseValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60016120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com