1

I have this function:

    function getNumOfSessionForTrainerClientList(req, res, rows, allowedToAddMoreClients, alertMessage) {

      let sessionData2 = null
      var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
      mysqlconn.connect(function(err) {
          if (err) {
            console.error('Database connection failed: ' + err.stack);
            return;
          }
        for (var i = 0; i < rows.length; i++) { 
        mysqlconn.query(getNumOfSessionForTrainerClientList, [rows[i].USERNAME, req.session.username], function(err, sessionData) {
            if (err) {
                console.log(err);
            } else {
                sessionData2 = sessionData

            }
           
        })
}
})
res.render('trainerclientlist.ejs', {data: rows, trainerFirstName: req.session.firstname, trainerLastName: req.session.lastname, allowedToAddMoreClients: allowedToAddMoreClients, profilePhoto: req.session.profilePhoto, alertMessage: req.session.selectedalertmessage, sessionData: sessionData2})
}

what this does is uses the results from another function, defined in rows, and then loops through them and queries my other table. However, I cannot figure out for the life of me how to pass the returned data sessionData to the front end (ejs). I am trying to pass all of the results and all the data inside the returned objects, but it is not working. On the front end, when I log sessionData.length, it returns length 0. How can i achieve this?

bobthebuilder
  • 75
  • 1
  • 6
  • whoever keeps deleting my question, can you stop. your reference question doesn't help. – bobthebuilder Feb 07 '22 at 05:41
  • It helps if you just read it. – Teemu Feb 07 '22 at 05:42
  • @Teemu I barley understand my own issue, thats why i'm asking the question. Would just appreciate a little bit of guidance as to what i may be doing wrong. Sending me to another question without any explanation or event comment as to what I may be doing wrong, or what the general scope of issue may be is not helpful and prevents others from helping. – bobthebuilder Feb 07 '22 at 05:50
  • In the answers of the linked duplicate there are several explanations and solutions of the general issue you're facing. It's not worth of explaining everything again and again because of just a bit different task and variable names. Also [this post](https://stackoverflow.com/q/16336367/1169519) might be useful to read. – Teemu Feb 07 '22 at 05:58

0 Answers0