S.N. S.N. - 1 year ago 97
Node.js Question

Printing mongoose collections in jade template; only getting one document

I’m new to web development in general and wonder why the the cursor method for mongoose only returns the first document in the collection and not all of them?

My goal is to pass these documents to a jade template.

I'm using MongoDB and Express

The code:

var array = [];
var stream = MyModel.find({}).cursor();


stream.on('error', function(err){
//Handle error

stream.on('close', function(){
res.render('dashboard', { title: 'Dashboard', csrfToken: req.csrfToken(), array: array});

Since I'm new to this my "code-jargon" might not be spot on so i'll be happy to specify if necessary.

Answer Source

You can try something like

 res.render('dashboard', { title: 'Dashboard', csrfToken: req.csrfToken(), array: docs});

according to mongoose doc, you can attach a callback to the query, so it will turn the cursor into an array of documents.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download