如何使用 EJS 和 Mongoose 在一个路径中显示两个表
我正在做一个简单的项目。我正在网上寻找解决方案,但找不到任何与我现在正在做的事情完全相同的东西。我的 MongoDB 中有一个集合表。我想显示两个过滤角色的显示。一张表用于管理员,一张表用于用户。
这是我来自 app.js 的代码
app.get("/", function (req, res) {
User.find({accountrole: "user"}, function(err, users) {
res.render('portal',{users:users});
// User.find({accountrole: "admin"}, function(err, admin) {
// res.render('portal',{admin:admin});
});
这仅适用于自从我将 accountrole: "user" 放在括号内后,就生成了一张表。请检查代码内的注释,我也想将其放入,这样我就可以有两张表,但可以过滤管理员和用户。
这是我的门户中的代码。埃杰斯
<table class="table table-hover citizen-table">
<thead class="table-dark">
<tr>
<!-- <th scope="col">#</th> -->
<th scope="col">Accout Role</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<% users.forEach(function(users){ %>
<tr>
<th scope="row"><%= users.accountrole %></th>
<td><%= users.firstname %></td>
<td><%= users.lastname %></td>
<td><%= users.email %></td>
</tr>
<% }); %>
</tbody>
</table>
I'm doing a simple project. I was searching for the solution online but I can't find anything that is exact to what I am doing right now. I have a collection table in my MongoDB. I want to display two displays that filters the role. One table for Admin, and then one table for User.
Please see the image of the example table collection
Here's my code from app.js
app.get("/", function (req, res) {
User.find({accountrole: "user"}, function(err, users) {
res.render('portal',{users:users});
// User.find({accountrole: "admin"}, function(err, admin) {
// res.render('portal',{admin:admin});
});
This works only in one table since I put the accountrole: "user" inside the bracket.Please check the comment inside the code,I wanna put it too so I can have two tables but filters the admin and user.
and then here's my code from my portal. ejs
<table class="table table-hover citizen-table">
<thead class="table-dark">
<tr>
<!-- <th scope="col">#</th> -->
<th scope="col">Accout Role</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<% users.forEach(function(users){ %>
<tr>
<th scope="row"><%= users.accountrole %></th>
<td><%= users.firstname %></td>
<td><%= users.lastname %></td>
<td><%= users.email %></td>
</tr>
<% }); %>
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
async/等待
语法,这很容易实现。获取用户,获取管理员,将两个对象一起传递给模板。This is very easy to achieve by using the
async/await
syntax. Fetch the users, fetch the admins, pass both objects together to your template.