Quantlawyer
Quantlawyer

Reputation: 11

Cannot iterate mongodb collection

I am trying to iterate through a MongoDb collection using Pug.

Console.log shows the collection items. Put iterates through the list but does not show any data.

Mongo Schema:

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

Controller:

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 });
  });
};

And in Pug:

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

Output shows two lines with the dots, but no data.

Upvotes: 1

Views: 48

Answers (1)

Darren G
Darren G

Reputation: 878

In your each loop in your pug view you should replace 'db_list' with 'user'. So it should be:

each user, index in db_list
      li= user.name

Upvotes: 2

Related Questions