html - 如何使用node js从mongodb检索用户完整数据

标签 html node.js mongodb express mongoose-schema

我在我的个人资料页面中获得了一个用户注册器详细信息,但如何在我的 html 个人资料页面中获取所有用户注册器详细信息?

这是我的一个用户注册详细信息从 mongodb 数据库获取代码

app.get('/profile', 函数(req, res) {

  User.findOne(function(err,user){
    if (err)
            return done(err);

        // check to see if theres already a user with that email
        if (user) {
          console.log("THIS : " + user.country);
          res.render('profile.ejs', {
          user: user
        });
  }

});

});

最佳答案

首先您需要查询所有用户。因此,您需要使用返回数组的 User.find,而不是 User.findOne

app.get('/profile', function(req, res) {
  User.find({}, function(err, Users){
    if (err)
        return done(err);

    if (Users) {
      console.log("Users count : " + user.length);
      res.render('profile.ejs', {
        usersArray: Users
      });
    }
  });
});

然后您需要设置您的 profile.ejs 来显示所有对象,为此您需要一个循环。

<h3>My users IDs are: </h3>
<ul>
  <% if (typeof usersArray !== 'undefined' && usersArray.length > 0) {%>
    <%for (var i = 0; i < usersArray.length; i++) { %>
      <li>ID: <%= usersArray[i]._id %></li>
    <%}%>
  <% } %>
</ul>

关于html - 如何使用node js从mongodb检索用户完整数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46502445/

相关文章:

node.js - 折旧警告不允许在 AZURE 上部署应用程序,

windows - 无法连接到远程 Azure Windows 计算机上运行的 MongoDB 实例

c++ - 如何将 MongoDB 与 Qt C++ 连接起来?

html - 响应式文本区域

html - 高度 :100% doesn't work for border

node.js - Azure Bot Framework(Node.js) - waterfall 在使用网络聊天的对话框中不起作用

node.js - 'TypeError : fs. existsSync 不是函数' ReactJS 和 Electron

javascript - 选择所有复选框以在 Gridview 中启用 boxex

html - 粘贴多行的快捷方式 - Sublime Text 2

javascript - MongoDB错误: Cannot create property '_id' on string