Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda