I am trying to learn how to read from databases and see if I can put everything into a file, however I hit a roadblock
const fs = require("fs")
const beautify = require("beautify")
exports.load = function(db) {
db.set("hello", {
"author": "Some author 1",
"title": "Blog Post 1",
"content": "First post content",
"date_posted": "Dec 17, 2021"
})
let postsArray = new Array();
db.list().then(keys => {
keys.forEach(async key => {
await db.get(key).then(value => {
console.log(value)
postsArray.push(`<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="/p">Anonymous</a>
<small class="text-muted">${ value.date_posted }</small>
</div>
<h2><a class="article-title" href="#">${ value.title }</a></h2>
<p class="article-content">${ value.content }</p>
</div>
</article>`
)
})
})
})
const posts = postsArray.join()
console.log(posts)
fs.writeFileSync("public/posts.ejs", posts)
}
I am trying to get the JSON object like shown in the db.set function. But when I log the final array it is empty. Even though I can see the object get printed in the console. I am new to promises and how it gets handled. Any help is appreciated.