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

125
Vistas
js sqlite3 gwait until the db returned all data before returning it to another function

what i want:
1 - client request data
2 - server.js requests dbcontroller.js for data
3 - dbcontroller.js requests db for data
4 - db returns data to db controller
5 - dbcontroller returns the data to server.js
6 - server.js responds with data to client

(dbcontroller.js row:4) on point 4 i have each so the data returns as packages
i think i have to assemble them before point 5, but then i have the problem that the function returns before even the first dataset is collected
i tried await but the ide sayd that await has no effect like this: "await db.each()"
i think it would be nice if i could get every row in one package from the database, but i have only the hope of a beginner

server.js code

router.get("/", function(req,res){ //on localhost:[port]
  var testData = dbcontroller.accessData();
  console.log("-------------------") //start log
  console.log(testData) //debugging purposes
  console.log("___________________") //end log
  res.send(testData) //send the data to the client
})

dbcontroller.js code

//Problem: testData returns only "testData" because db.each takes too long/ return does not wait for db.each to finish | await seems not helpful
function accessData(){
    var testData = "testData"; //wanted to save it here
    db.each("SELECT * FROM student", function(err, row) {
        if(err) return console.log(err.message);
        console.log(row.id + ": " + row.name);
        testData += " /n" + row.id + ": " + row.name; //the dataset gets addet do the string
    })
    return testData;
}
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