【发布时间】:2013-06-04 14:52:26
【问题描述】:
我是 node 和 mongodb 的新手,正在尝试在模板中呈现数据库查询,但我收到属性未定义的错误:
Cannot read property 'firstname' of undefined
这是我的 index.js 文件:
var dburl = 'localhost/olpdb';
var collections = ['users'];
var db = require('mongojs').connect(dburl, collections);
var currentUsers = db.users.find();
exports.index = function(req, res){
res.render('index', currentUsers);
};
在 index.jade 模板中,我有:
#{currentUsers.firstname}
我已经单独查询过数据库,知道有记录:
> db.users.find()
{ "firstname" : "andy", "lastname" : "johnson", "email" : "andy@johnson.com", "_id" : ObjectId("51adf8a8c58996cf0c000001") }
谁能帮我解决我做错了什么?我正在尝试将对象传递给模板,以便提取数据。
【问题讨论】: