我试图在客户端显示我的MongoDB数据库中的一些数据,但我一直收到以下错误:8| li 9| #{victim.first-name} > 10| Cannot read property 'first' of undefined
我一直关注此tutorial,但由于某种原因,我一直在遇到错误信息......有什么想法吗?
我的app.js文件的相关部分:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('ds012345.mlab.com:12345/dbname');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// Make db accessible to router
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/users', users);
我的index.js文件:
/* GET datatesting page */
router.get('/datatesting', function(req, res) {
var db = req.db;
var collection = db.get('data');
collection.find({},{},function(e,docs){
res.render('datatesting', {
"datatesting" : docs
});
});
});
我的datatesting.jade文件:
extends layout
block content
h1.
List of Victims
ul
each victim, i in datatesting
li
#{victim.first-name}
编辑:这里是从Mongo shell检索到的一些示例数据。该集合中只有13个文档。
{
"_id": {
"$oid": "56e5f15f0e1fc43fe589f5a9"
},
"first-name": "Tanisha",
"last-name": "Anderson",
"age": 37,
"gender": "F",
"killed": "Y",
"image": "http://google.com",
"street-address": "Ansel Road, Cleveland, Ohio",
"city": "Cleveland",
"state": "Ohio",
"latitude": 41.52067,
"longitude": -81.6222,
"date": "11/1/2014"
}
答案 0 :(得分:0)
所以我明白了!事实证明我需要在我的.jade
文件中仔细检查我的缩进。
复制并重新粘贴上述教程中的示例后,我得到了这个:
extends layout
block content
h1.
Victims
ul
each user, i in datatesting
li
img(src= "#{user.image}")
如您所见,h1
标记不应与ul
标记对齐。之后,它能够正确打印出我的数据。