Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Mongodb collection in jade template

I'm new in node.js and mongodb. I set a mongodb collection called "article". I would like to display all the article of this collections in a jade template. I used this code :

server.js:

articles: db.article.find()

index.jade:

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

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

Does anyone know how can I make that working?

Thanks

like image 908
Guillaume le Floch Avatar asked Aug 18 '12 00:08

Guillaume le Floch


1 Answers

.find() is asynchronous. you are using it synchronously.

function(req, res, next) {
  db.articles.find().toArray(function(err, articles) {
    res.render('page', {
      articles: articles
    }
  })
}
like image 75
Jonathan Ong Avatar answered Oct 22 '22 17:10

Jonathan Ong