Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用NodeJS和Express从MongoDB检索数据_Javascript_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 使用NodeJS和Express从MongoDB检索数据

Javascript 使用NodeJS和Express从MongoDB检索数据,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,好的,在过去的几天里,我开始搞乱Node(因为我认为我应该学习一些实际有用的东西,可能会给我找份工作)。现在,我知道如何服务页面、基本路由等等。美好的但我想学习如何查询数据库中的信息 现在,我正试图建立一个应用程序,作为一个网络喜剧网站。因此,理论上,当我输入urlhttp://localhost:3000/comic/ 我的app.js文件中有以下代码: router.get('/', function(req, res) { var name = getName(); c

好的,在过去的几天里,我开始搞乱Node(因为我认为我应该学习一些实际有用的东西,可能会给我找份工作)。现在,我知道如何服务页面、基本路由等等。美好的但我想学习如何查询数据库中的信息

现在,我正试图建立一个应用程序,作为一个网络喜剧网站。因此,理论上,当我输入url
http://localhost:3000/comic/

我的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())
以在控制台中输出“undefined”,但我不知道为什么它没有得到查询在数据库中实际可以找到的唯一元素

我曾尝试在某处搜索,甚至在谷歌上搜索,但没有成功


我该如何从对象中获取参数名

NodeJs是异步的。你需要回拨电话或电话


getName
函数正在使用
db.test.find
对Mongo进行异步调用。您可以通过在异步函数之后添加
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
}
您需要提供对
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
    }
  });
}

这是可行的(在应用程序中不会崩溃,索引页在浏览器中呈现),但标题没有设置。至少对我来说不是,在我的测试中。标题变为空,默认为“localhost:3000”。您在回调中添加了render?我不明白您的意思。代码看起来和你的一样。从getName获取信息,将其分配给“name”,然后在res.render()中使用它。哦,不,对不起!我的错。我在getName调用之外使用res.render。它起作用了。
test
Renato
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
    }
  });
}