title: mongodb articles.length undefined
body: <p>I am building a nodejs, express, mongodb blog, with jade.</p>
  
  <p>My folder structure is:
  project/
          modules/
            views/
                  index.jade
          app.js
          articleprovider-memory.js
          articleprovider-mongodb.js</p>
  
  <p>when I run node app.js through console and go to the localhost port I get the TypeError:</p>
  
  <p>Cannot read property 'length' of undefined at jade.debug.unshift.lineno...</p>
  
  <p>In browser. Possibly referring to an anonymous function. </p>
  
  <p>Here is the Articleprovider-memory.js</p>
  
  <pre><code>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();
  
      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>
  
  <p>articleprovider-mongodb.js</p>
  
  <pre><code>ArticleProvider = function(host, port) {
    this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {}));
    this.db.open(function(){});
  };
  
  ArticleProvider.prototype.save = function(articles, callback) {
      this.getCollection(function(error, article_collection) {
        if( error ) callback(error)
        else {
          if( typeof(articles.length)=="undefined")
            articles = [articles];
  
          for( var i =0;i&lt; articles.length;i++ ) {
            article = articles[i];
            article.created_at = new Date();
          }
  
          article_collection.insert(articles, function() {
            callback(null, articles);
          });
        }
      });
  };
  
  exports.ArticleProvider = ArticleProvider;
  </code></pre>
  
  <p>And this is my route:</p>
  
  <pre><code>var articleProvider = new ArticleProvider('localhost', 27017);
  
  app.get('/', function(req, res){    
      articleProvider.findAll( function(error,docs){        
          res.render('index.jade', {title: 'Blog', articles:docs});
              })
  res.render('index.jade')
  });
  </code></pre>
  
  <p>then the index.jade file</p>
  
  <pre><code>// extends layout
  
  block content
  h1= title
   #articles
      - each article in articles
        div.article
          div.created_at= article.created_at
          div.title 
              a(href="/blog/"+article._id.toHexString())!= article.title
          div.body= article.body
  </code></pre>
  
  <p>I've read a lot about all of the dependencies, but am still new to them. From what I can gather any of these could be the problem, if I am right do tell me with a detailed remedy.</p>
  
  <ol>
  <li><p>My index.jade code is incorrect</p></li>
  <li><p>index.jade is referring to an array and my articles object is not an array</p></li>
  <li><p>mongodb does not have a proper connection established to the app</p></li>
  <li><p>I need to be using monk but I'm not </p></li>
  </ol>
  
  <p>some of my code comes from this article
  <a href="http://howtonode.org/express-mongodb" rel="nofollow">http://howtonode.org/express-mongodb</a></p>
  
  <p>Thankyou in advance</p>