TypeError: Cannot read property 'findOne' of undefined
<p>Here are the codes</p>

<pre><code>var mongo = require('mongodb');

var databaseName = 'Starter',
    collectionName = 'wines';

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

db = new Db(databaseName, server);

db.open(function(err, db) {
  if(!err) {
    console.log("Connected to 'winedb' database");
    db.collection(collectionName, {strict:true}, function(err, collection) {
        if (err) {
            console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
            populateDB();
        }
    });
  }
});

exports.findById = function(req, res) {
  var id = req.params.id;
  console.log('Retrieving wine: ' + id);

  db.collection(collectionName, function(err, collection) {
      collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
          res.send(item);
      });
  });

};
</code></pre>

<p>These codes are based on a sample of restful api for nodejs with mongoDB.
However, it seems that it cannot recognise the function <code>findOne</code>. Can someone point out the problem? </p>

<p>Error message:</p>

<pre><code>TypeError: Cannot read property 'findOne' of undefined
</code></pre>