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

124
Views
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
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!