[英]node js with express not loading mongodb database content
Trying to learn MongoDB for Node.js 尝试学习MongoDB for Node.js
I've my app.js
: 我有我的
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: startup
集合中也有文档:
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: 但是,结果显示:
Can someone point me, where am I wrong? 有人可以指出我在哪里吗?
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. 您将启动集合作为
name
属性传递,但是出于某种原因,您在模板中尝试使用startup
标识符访问它。
Change either and you're good. 改变其中之一就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.