title: Retrieving data from MongoDB using NodeJS with Express
body: <p>Okay, so in the past few days I started messing with Node (because I think I should learn something that is actually useful and might get me a job). Right now, I know how to serve pages, basic routing and such. Nice. But I want to learn how to query databases for information.</p>
  
  <p>Right now, I'm trying to build an app that serves as a webcomic website. So, in theory, the application should query the database when I type in the url <code>http://localhost:3000/comic/&lt;comicid&gt;</code></p>
  
  <p>I have the following code in my app.js file:</p>
  
  <pre><code>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;
          }
      });
  }
  </code></pre>
  
  <p>With this setup I get <code>console.log(getName())</code> to output "undefined" in the console, but I have no idea <strong>why</strong> it doesnt get the only element that the query can actually find in the database. </p>
  
  <p>I have tried searching in SO and even for examples in Google, but no success.</p>
  
  <p>How the hell am I supposed to get the parameter name from the object? </p>