title: Node.js Error: Object function has no method
body: <p>I just started getting into node.js and followed this great tutorial on node.js at <a href="http://howtonode.org/express-mongodb" rel="nofollow">Howtonode</a>, express and mongod. However, I seem to be getting an error that no one else commented on in the comments. The latest comment was about a month ago, so maybe the code is outdated?</p>
  
  <p>The problem is that when I visit <code>http://localhost:3000/</code>, the page shows <code>Internal Server Error</code> and in the terminal, I get the error message below. Does anyone know what happened?</p>
  
  <p>Here's the error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>TypeError: Object function (){} has no method 'findAll'
      at Router.&lt;anonymous&gt; (/Users/x/nodejs/howtonode/blog/app.js:30:18)
      at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
      at middleware (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:244:9)
      at param (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:227:11)
      at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
      at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
      at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
      at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
      at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
      at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
  TypeError: Object function (){} has no method 'findAll'
      at Router.&lt;anonymous&gt; (/Users/x/nodejs/howtonode/blog/app.js:30:18)
      at done (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:250:22)
      at middleware (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:244:9)
      at param (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:227:11)
      at pass (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:232:6)
      at Router._dispatch (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:255:4)
      at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/express/lib/router/index.js:45:10)
      at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
      at Object.handle (/Users/x/nodejs/howtonode/blog/node_modules/stylus/lib/middleware.js:187:7)
      at next (/Users/x/nodejs/howtonode/blog/node_modules/express/node_modules/connect/lib/http.js:203:15)
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>var express = require('express');
  var ArticleProvider = require('./articleprovider-memory').ArticleProvider;
  
  var app = module.exports = express.createServer()
  
  // Configuration
  app.configure(function() {
      app.set('views', __dirname + '/views');
      app.set('view engine', 'jade');
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(require('stylus').middleware({ src: __dirname + '/public' }));
      app.use(app.router);
      app.use(express.static(__dirname + '/public'));
  });
  
  app.configure('development', function() {
      app.use(express.errorHandler({ dumpExceptions: true, showStakc: true }));
  });
  
  app.configure('production', function() {
      app.use(express.errorHandler());
  });
  
  var articleProvider= new ArticleProvider();
  
  
  // Routes
  app.get('/', function(req, res) {
      ArticleProvider.findAll(function(error, docs) {
          res.send(docs);
      });
  });
  
  app.listen(3000);
  </code></pre>
  
  <p><strong>article-provider-memory.js</strong></p>
  
  <pre><code>var articleCounter = 1;
  
  ArticleProvider = function(){};
  ArticleProvider.prototype.dummyData = [];
  
  ArticleProvider.prototype.findAll = function(callback) {
    callback( null, this.dummyData )
  };
  
  ArticleProvider.prototype.findById = function(id, callback) {
    var result = null;
    for(var i =0;i&lt;this.dummyData.length;i++) {
      if( this.dummyData[i]._id == id ) {
        result = this.dummyData[i];
        break;
      }
    }
    callback(null, result);
  };
  
  ArticleProvider.prototype.save = function(articles, callback) {
    var article = null;
  
    if( typeof(articles.length)=="undefined")
      articles = [articles];
  
    for( var i =0;i&lt; articles.length;i++ ) {
      article = articles[i];
      article._id = articleCounter++;
      article.created_at = new Date();
  
      if( article.comments === undefined )
        article.comments = [];
  
      for(var j =0;j&lt; article.comments.length; j++) {
        article.comments[j].created_at = new Date();
      }
      this.dummyData[this.dummyData.length]= article;
    }
    callback(null, articles);
  };
  
  /* Lets bootstrap with dummy data */
  new ArticleProvider().save([
    {title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
    {title: 'Post two', body: 'Body two'},
    {title: 'Post three', body: 'Body three'}
  ], function(error, articles){});
  
  exports.ArticleProvider = ArticleProvider;
  </code></pre>