title: Why does this function wont return the results?
body: <p>I'm writing my own class to manage mongodb queries within express framework.</p>
  
  <p>This how the class looks like</p>
  
  <pre><code>var PostModel = function(){};
  
  PostModel.prototype.index = function(db){
    db.open(function(err,db){
      if(!err){
  
        db.collection('post',function(err,collection){
  
          collection.find().toArray(function(err,posts){
            if(!err){
              db.close();
              return posts;
            }
          });  
        });
      }
    });
  };
  </code></pre>
  
  <p>When I call this function:</p>
  
  <pre><code>// GET /post index action
  app.get('/post',function(req,res){
  
    postModel = new PostModel();
    var posts = postModel.index(db);
    res.json(posts); 
  
  });
  </code></pre>
  
  <p>I dont know why seems like the function index did not return anything.</p>
  
  <p>But if I change the index function like this</p>
  
  <pre><code>var PostModel = function(){};
  
  PostModel.prototype.index = function(db){
    db.open(function(err,db){
      if(!err){
  
        db.collection('post',function(err,collection){
  
          collection.find().toArray(function(err,posts){
            if(!err){
              db.close();
              console.log(posts);
            }
          });  
        });
      }
    });
  };
  </code></pre>
  
  <p>NOTE the console.log instead of the return. With this changes I can see into the terminal all the post I wanted. That's beacause the function retrive all the post as it should.</p>
  
  <p>The problem is that it does not return the post :(</p>