title: Node.js and mongodb TypeError: undefined is not a function
body: <p>I am new to node.js and mongodb. I am using express and jade for a test app i am writing. I have configured mongodb and node.js but when I try and retrieve data from the database, I get the following error:  <code>TypeError: undefined is not a function</code></p>
  
  <pre><code>/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/base.js:242
              throw message;      
                    ^
      TypeError: undefined is not a function
          at commandHandler (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/cursor.js:706:16)
          at /Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/db.js:1806:9
          at Server.Base._callHandler (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
          at /Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
          at MongoReply.parseBody (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
          at null.&lt;anonymous&gt; (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/server.js:443:20)
          at EventEmitter.emit (events.js:95:17)
          at null.&lt;anonymous&gt; (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:191:13)
          at EventEmitter.emit (events.js:98:17)
          at Socket.&lt;anonymous&gt; (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/connection.js:418:22)
  </code></pre>
  
  <p>Here is my code:
  This is the userlist route that is responsible for fetching the data.</p>
  
  <pre><code>exports.userlist = function(db) {
      return function(req, res) {
          var collection = db.get('usercollection');
          collection.find({},{},function(e,docs){
              res.render('userlist', {
                  "userlist" : docs
              });
          });
      };
  };
  </code></pre>
  
  <p>Here is the userlist.jade file:</p>
  
  <pre><code>extends layout
  
  block content
      h1.
          User List
      ul
          each user, i in userlist
              li
                  a(href="mailto:#{user.email}")= user.username
  </code></pre>
  
  <p>Here is the code where I call userlist i.e app.js</p>
  
  <pre><code>/**
   * Module dependencies.
   */
  
  var express = require('express');
  var routes = require('./routes');
  var user = require('./routes/user');
  var http = require('http');
  var path = require('path');
  var mongo = require('mongodb');
  var monk = require('monk');
  var db = monk('localhost:27017/nodetest1');
  
  var app = express();
  
  // all environments
  app.set('port', process.env.PORT || 3000);
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(require('stylus').middleware(path.join(__dirname, 'public')));
  app.use(express.static(path.join(__dirname, 'public')));
  
  // development only
  if ('development' == app.get('env')) {
    app.use(express.errorHandler());
  }
  
  app.get('/', routes.index);
  app.get('/users', user.list);
  app.get('/helloworld', routes.helloworld);
  app.get('/userlist', routes.userlist(db));
  
  http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
  });
  </code></pre>