student student - 1 year ago 97
Node.js Question

node js with express not loading mongodb database content

Trying to learn MongoDB for Node.js

I've my


var express = require('express'),
app = express(),
engines = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
assert = require('assert');

app.engine('html', engines.nunjucks);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

MongoClient.connect('mongodb://localhost:27017/startup', function(err, db) {

assert.equal(null, err);
console.log("Successfully connected to MongoDB.");

app.get('/', function(req, res) {

db.collection('startup').find({}).toArray(function(err, docs) {
res.render('startup', {
'name': docs



app.use(function(req, res) {

var server = app.listen(3000, function() {
var port = server.address().port;
console.log('Express server listening on port %s.', port);


There're documents in the
collection as well:

documents in the startup collection

And the template file is:

{% for s in startup %}
<li><a href="{{ }}">{{ }}, {{ s.Founded }}</a>
{% else %}
<li>No startups found.</li>
{% endfor %}

However, the result says:

no startups found

Can someone point me, where am I wrong?

Answer Source

You're passing the startups collection as a name property, but in a template you try to access it with startup identifier for some reason.

Change either and you're good.

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