【发布时间】:2014-05-19 09:11:45
【问题描述】:
我在express+EJS中有一些代码,
1) 在 app.js 中,创建了 mongo 集合对象,
app.locals.userCollection = db.get('userData');
2) 在 user.js express route 文件中,我从这个 DB 连接中获取数据并希望将其传递给 EJS 进行渲染,
exports.list = function(req, res){
req.app.locals.userCollection.find({},function(err , returnValue){
if(returnValue.length>0){
res.render('user', res.locals.returnValue);
}
return err;
});
};
3) 在 user.ejs 中,我尝试使用
访问它<div><script>
var test = <%- returnValue %>;
它给了我 returnValue is not defined 错误。
我可以知道是否要访问 returnValue[0].attr1,我应该在路由和 EJS 中编写什么代码?
问候 锤子
【问题讨论】: