【发布时间】:2015-07-20 16:45:47
【问题描述】:
我正在开发一个节点项目。如果我问了很多愚蠢的问题,node 对我来说很新,很抱歉。
我正在尝试在我的 index.ejs 上显示我的所有主题。根文件已在控制台日志中显示它们,但我似乎无法让它们显示在视图中。
视图已经循环并且知道有 2 个主题,但是内容是空的。
这是我在 routes/index.js 中的代码
router.get('/', function(req, res, next) {
Topic.find({}).exec(function(err, topic)
{
if(err){
console.log('There ware no topics');
return next(err)
}
else
{
console.log('Whoop whoop there are some topics');
res.render('index', { topic: topic } );
console.log("Logging data: " + topic);
console.log("Loggin data title out db: " + topic.topicTitle);
console.log("Loggin data desc out db: " + topic.topicDescription);
console.log("Loggin data date out db: " + topic.topicDateCreated);
}
});
});
module.exports = router;
这是我尝试在我的views/index.ejs中展示它的方式
<ul>
<% for(var i = 0; i < topic.length; i++) {%>
<li>
<div class="post animated fadeInLeft">
<div class="wrap-ut pull-left">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="default avatar" />
<p class="moderator"> <%= topic.fbUsername %> </p>
</div>
</div>
<div class="posttext pull-left">
<h2 class="topictitle"><a href="/topicdetail/{topic_id}" <%= topic.topicTitle %> </a></h2>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</li>
<% } %>
</ul>
【问题讨论】:
标签: node.js mongodb loops view ejs