【问题标题】:Mongoose find() not show my output in console.logMongoose find() 不在 console.log 中显示我的输出
【发布时间】:2016-03-07 02:40:40
【问题描述】:

我正在使用 node、express、mongo 开发一个项目,我正在尝试从数据库中查询我的所有主题。

我相信我的代码是正确的,但是当我在浏览器中运行应用程序时,我的 console.log 没有显示我的输出。

这是我的路线代码:

// show all topics 
router.get('/', function(req, res, next) {

var db = req.db;
console.log('Loggin DB: ' + db);

Topic.find({}).exec(function(err, topics)
{
    if(err)
    {
        return next(err)
        console.log("Logging the error: " + err);
        console.log('There were no topics based on your current location');
     }
     else
     {
        console.log('Whoop whoop we found topics near your location');
        //renders the data on the page
        res.render('index', { topic : topic } );
        console.log("Logging data: " + topic);
        console.log("Logging data title: " + topic.topicTitle);
     }
});
});

【问题讨论】:

  • 如果出现错误,return 将阻止您访问console.log
  • 就是没有错误
  • @GY22 "我相信我的代码是正确的" - 它不正确
  • @Dyrk,如果它不正确的话。是否可以为我指明正确的方向...
  • 您在 find 回调函数中返回 topics,但您将 topic 传递给渲染函数。

标签: node.js express mongoose find


【解决方案1】:

你正在进入主题s

Topic.find({}).exec(function(err, topics)

然后你正在使用主题(它不存在)

res.render('index', { topic : topic } );
    console.log("Logging data: " + topic);
    console.log("Logging data title: " + topic.topicTitle);

【讨论】:

  • 谢谢。我忽略了错字。但它仍然没有记录我的 console.logs ...
【解决方案2】:

您正在服务器端使用 console.log。这不会在浏览器中显示客户端,而是在运行节点 js 的命令提示符/终端中显示。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多