简体   繁体   English

试图在node.js app中显示来自mongodb数据库的数据

[英]Trying to display data from mongodb database in node.js app

server.get('/', function(req, res) {
    Counter.find({}, function(err, result) {
        if (!(err)) {
            res.render('index', {'lol' : result});
        }
    });
});

I'm trying to get my app to display the contents of the whole database and this is what I came up with. 我正在尝试让我的应用程序显示整个数据库的内容,这就是我想出的。 Counter is a mongoose model. 计数器是猫鼬模型。 The database contains some items inserted prior to the execution of the program and one item inserted in the app itself. 数据库包含在执行程序之前插入的一些项目以及在应用程序本身中插入的一个项目。

Something is really iffy conceptually to me (I'm new to node), I think render() is being executed before find() which is why I'm not getting a result, but I can't think of a solution. 在概念上对我来说真是不确定(我是节点的新手),我认为在find()之前正在执行render(),这就是为什么我没有得到结果,但我想不出解决方案。 Any help or push in the right direction is greatly appreciated. 非常感谢任何帮助或推动正确的方向。 :) :)

What is your view code? 你的观点代码是什么?

Your implementation is correct, you should try to do debug. 您的实现是正确的,您应该尝试进行调试。

server.get('/', function(req, res) {
  Counter.find({}, function(err, result) {
    if (!(err)) {
        console.log('Debug: ' + JSON.stringify(result) );
        res.render('index', {lol : result});
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM