title: nodejs mongodb cursor problems
body: <p>I have this function that I want to return the value of a mongo query but I am getting undefined values. I am attempting to read the Email address of all of the users in the users collection of my test database.</p>
  
  <p>This is my code:</p>
  
  <pre><code>var mongo = require('mongodb').MongoClient;
  var connectionString = "mongodb://cannotdivulge.info.com:53778/testdb";
  var users;
  var db = mongo.connect(connectionString, function(err, db) {
      if(err)
          throw err;
      console.log("connected to database");
      users = db.collection('users');
  });
  
  exports.findAll = function(req, res) {
      var cursor = users.find();
      var result;
      cursor.each(function(err, doc) {
          if(err)
              throw err;
          result = doc.Email;
      });
      res.send(result);
  };
  </code></pre>
  
  <p>result should be equal to the email address but it's undefined. The value of doc.Email is not being preserved. When I place res.send() inside cursor.each(), it will return a value.</p>
  
  <p>Also, I will need to use the result variable in the function before actually returning it's value. That is why I need it's value to be preserved.</p>