Showing Mongodb Collection in Jade Pattern

I am new to node.js and mongodb. I installed a mongodb collection called "article". I would like to show the entire article of these collections in a jade pattern. I used this code:

server.js:

articles: db.article.find()

index.jade:

-for article in articles
      .row
        .twelve.columns
          .panel
            li= article.text

Jade is really basic, but that will change. The fact is that when I run this code, the list in the jade template is empty and nothing is displayed. It looks like the variable 'articles' is empty.

Does anyone know how I can make this work?

thank

+5
source share
2 answers

.find()is asynchronous. you use it synchronously.

function(req, res, next) {
  db.articles.find().toArray(function(err, articles) {
    res.render('page', {
      articles: articles
    }
  })
}
+8
source

to try:

- console.log(articles)

In the template to find out if the array is empty!

0
source

All Articles