【问题标题】:Making message load from db with socket.io使用 socket.io 从数据库加载消息
【发布时间】: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

所以我不确定我的代码哪里出错了。

【问题讨论】:

标签: node.js mongodb sockets express


【解决方案1】:

你能用 find 代替 where first 吗?

Comment.find({commentTopicId: topic._id}).exec(function(err,  comments) {
  res.send(200, comments);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2021-11-15
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多