简体   繁体   中英

node.js and ejs requesting html id or name to populate variable

I am trying to access a specific id from a page but keep getting an undefined error. I am a little over my head with this but if anyone could point me in the right direction it would be much appreciated.

Basically, I just want to loop through all projects in my database, all interviewers in my database and pass back interviews and hours into the database.

    sumIntsHours(req, res){ 
    var dbRequest = 'SELECT * FROM `projects` where `status` = 2 ORDER BY jobName ASC';
    db.query(dbRequest, function(error, rows) {
        if(rows.length !== 0) {
            projects = rows;
        }   
    dbRequest = 'SELECT * FROM `interviewers` ORDER BY intName ASC';
        db.query(dbRequest, function(error, rows) {
            interviewers = rows;
        interviewers.forEach((interviewer) => {
            projects.forEach((project) => {
                let hours = req.body.jobHours + "_" + project.id + "_" + interviewer.id;
                let interviews = req.body.jobInts + "_" + project.id + "_" + interviewer.id;
                let jobID = project.id;
                let query = "UPDATE projects SET hours = hours + " + hours + ", interviews = interviews + " + interviews + " WHERE projects.id = '" + jobID + "'";
            db.query(query, (err, result) => {
                    if (err) {
                    return res.status(500).send(err);
                            }
                    res.redirect('/');
            });
});
});
});
});
},
}

Here's my EJS

<% include partials/header.ejs %>
<div class="table-wrapper">
  <% if (projects.length > 0) {%>
   <form class="add-daily-form" action="" method="post" enctype="multipart/form-data">
    <table class="table table-hovered" id="daily-table">
      <thead class="thead-dark">
        <tr>
          <th scope="col" style="width:10%;">Interviewers</th>
          <% projects.forEach((project, index) => { %>
            <th scope="col" style="width:10%;"><%= project.jobNum %> <%= project.jobName %></th>
            <% }) %>
          </tr>
        </thead>
        <tbody>
        </thead>
        <% interviewers.forEach((interviewer, index) => { %>
          <tr>
            <td><%= interviewer.intName %></td>
            <% projects.forEach((project, index) => { %>
              <td>
                <input style="display:inline-block; width:48%" type="number" placeholder="Hours" class="form-control" name="jobHours_<%= project.id %>_<%= interviewer.id %>" id="jobHours_<%= project.id %>_<%= interviewer.id %>"  step="0.01">
                <input style="display:inline-block; width:48%" type="number" placeholder="Interviews" class="form-control" name="jobInts_<%= project.id %>_<%= interviewer.id %>" id="jobInts_<%= project.id %>_<%= interviewer.id %>" step="0.01">
              </td>
              <% })%>
              <% })%>
              </tr>
              <tr>
              <td> </td>
                    <% projects.forEach((project, index) => { %>
              <td>
                <input style="display:inline-block; width:48%" type="number" placeholder="Total Hours" class="form-control" name="TjobHours_<%= project.id %>" id="TjobHours_<%= project.id %>" step="0.01">
                <input style="display:inline-block; width:48%" type="number" placeholder="Total Interviews" class="form-control" name="TjobInts_<%= project.id %>" id="TjobInts_<%= project.id %>" step="0.01">
              </td>
                    <% })%>
              </tr>
            </tbody>
          </table>
          <% }%>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
        </div>
      </div>
    </body>
    </html>

The error I am facing (at the moment) seems to come from this bit

                let hours = req.body.jobHours + "_" + project.id + "_" + interviewer.id;
                let interviews = req.body.jobInts + "_" + project.id + "_" + interviewer.id;

I am attempting to reference these input fields but not sure how the request should be formatted when looped like this.

                <input style="display:inline-block; width:48%" type="number" placeholder="Hours" class="form-control" name="jobHours_<%= project.id %>_<%= interviewer.id %>" id="jobHours_<%= project.id %>_<%= interviewer.id %>"  step="0.01">
                <input style="display:inline-block; width:48%" type="number" placeholder="Interviews" class="form-control" name="jobInts_<%= project.id %>_<%= interviewer.id %>" id="jobInts_<%= project.id %>_<%= interviewer.id %>" step="0.01">

I can also see by logging req.body that all the required data exists in the format I'm trying to use (example: jobHours_20_8) but I'm currently struggling to access it.

Solved this.

            var z = project.id;
            var y = interviewer.id;
            var n = ("jobInts_"+z+"_"+y);
            var m = ("jobHours_"+z+"_"+y);
            let hh = req.body[m]; 
            let ii = req.body[n]; 
            let query = "UPDATE projects SET hours = hours + " + hh + ", interviews = interviews + " + ii + " WHERE projects.id = '" + jobID + "'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM