我如何将所有用户名从Firestore传递到ejs以便在我的users.ejs上显示

问题描述 投票:0回答:1

我已经从数据库中获取了所有数据,但是我想将其显示到我的用户页面(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'});
});
node.js google-cloud-firestore ejs
1个回答
0
投票

您可以如下所述传递整个数组,并可以使用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>

希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.