【问题标题】:Retrieving data from MongoDB using NodeJS with Express使用带有 Express 的 NodeJS 从 MongoDB 检索数据
【发布时间】:2015-01-17 00:46:23
【问题描述】:

好的,所以在过去的几天里,我开始弄乱 Node(因为我认为我应该学习一些真正有用的东西,并且可能会给我找到一份工作)。现在,我知道如何提供页面、基本路由等。好的。但我想学习如何从数据库中查询信息。

现在,我正在尝试构建一个用作网络漫画网站的应用。因此,理论上,当我输入 url http://localhost:3000/comic/<comicid> 时,应用程序应该查询数据库

我的 app.js 文件中有以下代码:

router.get('/', function(req, res) {  
    var name = getName();
    console.log(name); // this prints "undefined"

    res.render('index', {
        title: name,
        year: date.getFullYear()
    });
});

function getName(){
    db.test.find({name: "Renato"}, function(err, objs){
    var returnable_name;
        if (objs.length == 1)
        {
            returnable_name = objs[0].name;
            console.log(returnable_name); // this prints "Renato", as it should
            return returnable_name;
        }
    });
}

通过这个设置,我得到console.log(getName()) 在控制台中输出“未定义”,但我不知道为什么它没有得到查询可以在数据库中实际找到的唯一元素。

我曾尝试在 SO 中搜索,甚至在 Google 中搜索示例,但没有成功。

我到底应该如何从对象中获取参数名称?

【问题讨论】:

    标签: javascript node.js mongodb express


    【解决方案1】:

    NodeJs 是异步的。您需要回电或Promise

    router.get('/', function(req, res) {
        var name = '';
        getName(function(data){
            name = data;
            console.log(name);
    
            res.render('index', {
                title: name,
                year: date.getFullYear()
            });
        });
    });
    
    function getName(callback){
        db.test.find({name: "Renato"}, function(err, objs){
            var returnable_name;
            if (objs.length == 1)
            {
                returnable_name = objs[0].name;
                console.log(returnable_name); // this prints "Renato", as it should
                callback(returnable_name);
            }
        });
    }
    

    【讨论】:

    • 这可行(因为在应用程序中不会崩溃,并且索引页面会在浏览器中呈现)但未设置标题。至少对我来说不是,在我的测试中。标题只是变成空的,默认为“localhost:3000”。
    • 你在回调上添加了渲染?
    • 我不明白你的意思。代码看起来就像你的一样。从 getName 获取信息,将其分配给“name”,然后在 res.render() 中使用它。
    • 哦不,对不起!我的错。我在 getName 调用之外与 res.render 在一起。它有效。
    【解决方案2】:

    getName 函数正在使用db.test.find 对 Mongo 进行异步调用。您可以通过在 async 函数后添加 console.log 来看到这一点。像这样:

    function getName(){
      db.test.find({name: "Renato"}, function(err, objs){
        var returnable_name;
        if (objs.length == 1) {
          returnable_name = objs[0].name;
          console.log(returnable_name);
          return returnable_name;
        }
      });
      console.log('test'); // <!-- Here
    }
    

    这很可能会输出:

    test
    Renato
    

    您需要为您的getName 函数提供回调。

    router.get('/', function(req, res) {  
      getName(function(err, name) {
        res.render('index', {
            title: name,
            year: date.getFullYear()
        });
      })'
    });
    
    function getName(cb){
      db.test.find({name: "Renato"}, function(err, objs){
        if(err) cb(err);
        var returnable_name;
        if (objs.length == 1) {
          returnable_name = objs[0].name;
          return cb(null, returnable_name);
        } else {
          // Not sure what you want to do if there are no results
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2020-03-30
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多