0

Trying to learn MongoDB for Node.js

I've my app.js:

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
      });
      console.log(docs);

    });

  });

  app.use(function(req, res) {
    res.sendStatus(404);
  });

  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 startup collection as well:

documents in the startup collection

And the template file is:

<h1>startups</h1>
{% for s in startup %}
<li><a href="https://www.google.com/search?q={{ s.name }}">{{ s.name }}, {{ s.Founded }}</a>
</li>
{% else %}
<li>No startups found.</li>
{% endfor %}

However, the result says:

no startups found

Can someone point me, where am I wrong?

2
  • for s in name I guess... Commented Aug 9, 2016 at 4:29
  • @Rayon Nope, I tried it. Commented Aug 9, 2016 at 4:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @zerkms!
@student – Kindly accept and up-vote the solution which has solved the purpose :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.