Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Empty array even if the data is received

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!