【发布时间】:2017-03-28 05:04:43
【问题描述】:
我一直在使用 mongoose 成功地在 ejs 页面上呈现来自数据库的信息。现在我想渲染在特定对象上填充的信息(通过 mongo 填充)。这甚至可能吗?到目前为止我还没有成功
app.get('/profile', isLoggedIn, function(req, res) {
Event.find(function(err, services) {
if (err) return next(err);
services.forEach(function(service){
Event
.findOne({title: service.title})
.populate('user')
.exec(function (err, populated) {
if (err) return handleError(err);
if(!populated) console.log("failed")
else {
console.log('The creator is ' + populated.user.local.name);
service = populated;
}
});
});
res.render('./partials/profile.ejs', {user: req.user, status: status, badges: badgerinos, services: services}); // load the profile.ejs partial
});
});
console.log 正确显示了创建者的姓名,但在渲染页面上,用户字段未定义。我究竟做错了什么?
【问题讨论】: