I can't access snapshot value outside promises. I have these functions
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();
})
}
and when I try this
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`)
this is what I get
Our post: undefined our categories: undefined total number of posts: undefined
I dont really know if it has anything to do with scope. Please help