【发布时间】:2020-10-04 07:53:42
【问题描述】:
我想显示时间表集合中的所有数据,但我希望数据只显示教室名称一个。例如,我有 2 个 classroom_name 字段,即 6 Usaha。我只想要 EJS 表中的 6 Usaha 中的 1 个。
知道了,系统已经根据 Collections 中的记录数显示数据,在这种情况下是 2。
实际结果
6 Usaha
6 Usaha
预期结果
6 Usaha
这是我的实现代码。
EJS 文件
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Class</th>
<th>Settings</th>
</tr>
</thead>
<tbody>
<% timetable.forEach(function (timetable) { %>
<tr>
<td><%= timetable.classroom.classroom_name %></td>
<td><a class="button" href="/timetable_class/view/<%= timetable.classroom._id %>"</a>View </td>
</tr>
<% }) %>
</tbody>
</table>
服务器文件
router.get('/timetable_class',mid, function(req,res){
Teacher.findById(req.session.userId).exec(function (error, user){
if (error){
return next(error);
}else
{
Timetable.find({year:currentYear}).populate('classroom').exec(function(err, timetable)
{
if (err) throw err;
console.log(currentYear);
console.log(timetable);
res.render('admin_content/view_timetable_table',{timetable:timetable, user:user});
});
}
});
});
【问题讨论】:
-
对于具有重复
classroom.classroom_name值的其他文档应该如何处理?您只想单独显示第一次出现吗? -
是的。我只想显示第一次出现。 @Tunmee
标签: node.js mongodb mongoose ejs