1

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;
}

0 Answers0