我正在尝试使用EJS作为模板引擎制作一个使用Node.js的待办事项应用程序。所有对象都在3个数组中:主题,日期和细节。它们在阵列中。
我在快递路线中使用此代码:
app.get('/dashboard', function (req, res) {
var subjects = [];
var duedates = [];
var details = [];
var ids = [];
var userId = 'some user provided value';
var sql = 'SELECT * FROM assignments WHERE owner = "' + req.user.email + '"';
connection.query(sql, function (error, results, fields) {
if (error) throw error;
for (i = 0; i < results.length; i++) {
subjects.push(results[i]['subject']);
duedates.push(results[i]['duedate']);
details.push(results[i]['details']);
ids.push(results[i]['id']);
console.log(details)
//res.render('dashboard',{username:req.user.email, subject: subjects, date: duedates, detail: details, id: ids});
} // ...
});
});
此代码在EJS模板上:
<!DOCTYPE html>
<html>
<head>
<title>Pencil and Pens</title>
</head>
<body>
<center>
<h1>Pencil and Pens</h1>
<h2>Welcome:
<%= username %>
</h2>
<form action="/addassignment" method="post">
Subject: <input type="text" id="subject" name="subject"> Due Date: <input type="duedate" name="duedate"> Details:
<input type="details" name="details">
<input type="submit" value="Add Assignment...">
</form>
<br />
<div id="assignmentbody">
<% for(var i=0; i < subject.length; i++) { %>
<%= subject[i] %> |
<%= date[i] %> |
<%= detail[i] %>
<form style="display:inline-block;" action="/deleteAssignment" method="post">
<% } %>
</div>
</center>
<script type="text/javascript">
</script>
</body>
</html>
即使阵列中有3个,也只显示第一个条目。
非常感谢!
本菲恩答案 0 :(得分:0)
通过阅读您的代码,我发现3个阵列主题, duedates 和详细信息名称不同我的意思是您错过了's'用于所有3个阵列,同时在中显示循环。添加这些。它会正常工作。