简体   繁体   English

node.js 和 ejs 请求 html id 或名称来填充变量

[英]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.我试图从页面访问特定 id,但不断收到未定义的错误。 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这是我的 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.我还可以通过记录 req.body 看到所有必需的数据都以我尝试使用的格式存在(例如:jobHours_20_8),但我目前正在努力访问它。

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 + "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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