首页 > 解决方案 > 无法迭代 mongodb 集合

问题描述

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

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

蒙戈模式:

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

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

标签: javascriptmongodbexpresspug

解决方案


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

each user, index in db_list
      li= user.name

推荐阅读