title: mongoose and node error : Cannot call method 'model' of undefined
body: <p>I tried following a tutorial on mongoose/mongodb with node and have fallen into issues trying to get express/node to <code>res.send</code> json documents from the collection.</p>
  
  <p>The following code is producing errors when I try to access localhost:3000/mongodb</p>
  
  <p>The database and collection exist. The collection has 3 documents.</p>
  
  <p>app.js</p>
  
  <pre><code>/**
   * Module dependencies.
   */
  
  var express = require('express');
  var http    = require('http');
  var path    = require('path');
  
  var mongoose = require('mongoose');
  var app = module.exports = express();
  
  // all environments
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.engine('html', require('hogan-express'));
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
  
  mongoose.connect('mongodb://localhost/xkayak');
  var schema         = new mongoose.Schema({ username: 'string', email: 'string', password: 'string'});
  var usercollection = mongoose.model('usercollection', schema);
  
  // development only
  if ('development' == app.get('env')) {
    app.use(express.errorHandler());
  }
  
  require('./routes/index.js');
  
  http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
  });
  </code></pre>
  
  <p>index.js --the routes file</p>
  
  <pre><code>var app = require('../app.js');
  
  app.get('/mongodb', function(req, res) {
      app.mongoose.model('usercollection').find(function(err, usercollection) {
          res.send(usercollection);
      });
  });
  </code></pre>
  
  <p>The error produced is:</p>
  
  <pre><code>500 TypeError: Cannot call method 'model' of undefined
  </code></pre>
  
  <p>What is wrong with my code? Did I set up the collection incorrectly? If I remove this code everything else works.</p>