我正在尝试了解express中的路由,我想在MongoDB数据库中检索文档。我已建立数据库连接如下:
//database object
var db
//Database Path
var dbpath = process.env.MONGODB_URI || 'mongodb://<dbuser>:<dbpassword>@ds163758.mlab.com:63758/heroku_xv7rsvvw';
//Establish database connection
MongoClient.connect(dbpath, (err, database) => {
if (err) return console.log(err)
db = database
app.listen(process.env.PORT || theport, () => {
console.log('listening on 3000')
})
})
我想从&#34;部分收集我的观点&#34;集合在上面的MongoDB中,所以在index.js中,然后将代码输出到jade / put模板中:
//find about
//2. Read the quotes from the collection
//3. Render them in index.ejs
app.get('/about', (req, res) => {
db.collection('sections').find({"name": "about"}).toArray((err, result) => {
if (err) return console.log(err)
// renders index.ejs
res.render('about.jade#content', {sections: result})
console.log(result)
})
})
模板(about.jade)
extends layout
block content
h1= title
section
h2 #{content}
部分文件:
{
"_id": {
"$oid": "587dfdgf36d284ed58a9f69"
},
"name": "about",
"tittle": "About Us",
"summary": "WE KNOW HOW TO BUILD GREAT PRODUCTS",
"content": "<p>Some features of the app and releasing it to the app store won\u2019t make it great. Lorem ipsom \u2019ll dolor bensulplin \u2013 hnlmops
我做错了什么?