Reputation: 1083
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
Upvotes: 6
Views: 4046
Reputation: 20315
.find()
is asynchronous. you are using it synchronously.
function(req, res, next) {
db.articles.find().toArray(function(err, articles) {
res.render('page', {
articles: articles
}
})
}
Upvotes: 8
Reputation: 6339
try:
- console.log(articles)
In your template to figure out if the array is empty!
Upvotes: 0