0
我無法訪問承諾外的快照值。我有這些功能無法訪問承諾外的Firebase數據庫快照值
function getAuthorData() {
metadataRef
.child("author")
.on("value", (snap) => {
return snap.val()
})
}
function getSiteSocialLinks() {
metadataRef
.child("social")
.on('value', (snap) => {
return snap.val();
})
}
function getFeaturedPosts() {
featuredRef.on('value', (snap) => {
return snap.val();
})
}
function getBlogPosts() {
blogPostsRef
.orderByChild('createOn')
.on('value', (snap) => {
return snap.val()
});
}
function getPostCategories() {
postRef
.child('categoryStats')
.on('value', (snap) => {
return snap.val();
});
}
function getTotalPostsLength() {
postRef
.child('nopp')
.on('value', (snap) => {
return snap.val();
})
}
,當我嘗試這個
var ourPosts = getBlogPosts()
var ourCategories = getPostCategories()
var totalnumOfPosts = getTotalPostsLength()
console.log(`Our post: ${ourPosts}\n our categories: ${ourCategories}\n total number of posts: ${totalnumOfPosts}\n`)
這就是我得到
我們的崗位:未定義我們的類別:未定義的消息總數: 未定義
我真的不知道它是否有任何東西與範圍有關。請幫助
異步代碼會把你弄得像每一次 –
那麼出路是什麼? – Jalasem
使用承諾,因爲它們是爲了使用,也許?由於你的函數不返回任何東西,這有點困難 –