【问题标题】:Cannot iterate mongodb collection无法迭代 mongodb 集合
【发布时间】:2019-09-01 23:46:23
【问题描述】:

我正在尝试使用 Pug 遍历 MongoDb 集合。

Console.log 显示收集项目。 Put 遍历列表但不显示任何数据。

Mongo 架构:

mongoose.Schema({
  name: String,
  email: String,
});

控制器:

exports.listDB = (req, res) => {
  Db.find({}, (err, users) => {
    if (err) throw err;
    console.log(users); // this shows the collection with id, name, email
    res.render('dblist', { title: 'Database List', db_list: users });
  });
};

在帕格:

extends layout

block content
  h1 List Database Records
  p.lead This page will show all the records in the database
  hr
  ul
    each user, index in db_list
      li= db_list.name

输出显示两行带点,但没有数据。

【问题讨论】:

    标签: javascript mongodb express pug


    【解决方案1】:

    在您的哈巴狗视图的每个循环中,您应该将“db_list”替换为“用户”。所以应该是:

    each user, index in db_list
          li= user.name
    

    【讨论】:

    • @Gui Code 如果回答有帮助,请采纳。谢谢:)
    猜你喜欢
    • 2017-06-18
    • 2012-09-25
    • 1970-01-01
    • 2021-12-14
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多