【发布时间】:2015-05-13 05:54:00
【问题描述】:
我正在使用 node、express、mongo 和 socket.io 开发一个项目。我成功建立了套接字连接并将消息保存到数据库。我现在想要实现的是,当有人加入讨论时,他可以看到所有以前的消息。这是我遇到障碍的地方。
我的路由文件代码如下:
router.get('/:id', function(req, res, next) {
var topicId = req.params.id;
console.log('Logging topic id: ' + topicId);
// find one single topic based on id
Topic.findById(topicId, function(err, topic)
{
if(err)
{
console.log('There was no topic with this ID');
return next(err)
}
else
{
console.log('Whoop whoop we found a topic matching the requested ID');
Comment.find().where("commentTopicId", topicId).exec(function(err, comments)
{
res.render('topicdetail', { topic: topic , comments: comments } );
console.log("Logging data topic: " + topic);
console.log("Loggin data title out db: " + topic.topicTitle);
console.log("Logging data comment: " + comments);
console.log("Logging error: " + err);
});
}
});
});
else 语句中的第二个查询有效,因为我获得了包含必要信息的主题详细信息页面,但 cmets 的控制台日志返回 null
所以我不确定我的代码哪里出错了。
【问题讨论】:
-
你的前端是什么样子的?
-
我还没有在视图中为我首先想在控制台日志中获取数据的 cmets 创建 for 循环 (qa-forum.herokuapp.com/topicdetail/555241db169ed40300991166)
标签: node.js mongodb sockets express