我已经从数据库中获取了所有数据,但是我想将其显示到我的用户页面(users.ejs)
router.get('/', function(req, res, next) {
let usersData = db.collection('users');
let query = usersData.get()
.then(snapshot => {
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(doc => {
console.log(doc.data())
});
})
.catch(err => {
console.log('Error getting documents', err);
});
res.render('users', {title: 'Users'});
});
您可以如下所述传递整个数组,并可以使用ejs模板中的循环显示它:
res.render('users', {
users: users // Array of all the users.
});
Users.ejs
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<header>
<% include ../partials/header %>
</header>
<div class="grid">
<div class="col-1-1">
<div class="body-content">
<% users.forEach(function(user) { %>
<h1><%= user.name %></h1>
<% }); %>
</div>
</div>
</div>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>
希望对您有帮助!