mongodb articles.length未定义

我正在用jade构build一个nodejs,express,mongodb博客。

我的文件夹结构是:project / modules / views / index.jade app.js articleprovider-memory.js articleprovider-mongodb.js

当我通过控制台运行节点app.js并转到localhost端口时,我得到TypeError:

无法读取jade.debug.unshift.lineno未定义的属性“长度”…

在浏览器中。 可能是指一个匿名函数。

这里是Articleprovider-memory.js

ArticleProvider.prototype.save = function(articles, callback) { var article = null; if( typeof(articles.length)=="undefined") articles = [articles]; for( var i =0;i< 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; 

articleprovider-mongodb.js

 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< articles.length;i++ ) { article = articles[i]; article.created_at = new Date(); } article_collection.insert(articles, function() { callback(null, articles); }); } }); }; exports.ArticleProvider = ArticleProvider; 

这是我的路线:

 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') }); 

然后是index.jade文件

 // 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 

我已经读了很多关于所有的依赖,但对他们来说还是新的。 从我能收集到的任何一个都可能是问题,如果我是对的,请告诉我一个详细的补救措施。

  1. 我的index.jade代码是不正确的

  2. index.jade指的是一个数组,我的articles对象不是一个数组

  3. mongodb没有build立到应用程序的正确连接

  4. 我需要和尚,但我不是

我的一些代码来自这篇文章http://howtonode.org/express-mongodb

先谢谢你

1.修复快速路线

您的路线有多个render调用。 它应该被修改为。

 app.get('/', function(req, res){ articleProvider.findAll( function(error,docs){ res.render('index.jade', {title: 'Blog', articles:docs}); }) }); 

2.循环前检查articles在玉视图中定义

在遍历articles数组之前的jade视图中,确保已经定义了它。

 block content h1= title #articles - if(typeof(article) !== 'undefined') - 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 

3.在mongo查询中处理错误参数

你也考虑了callback中的errorvariables。 所以如果在查询mongo的时候发生了什么错误,那么就可以处理了。 喜欢

 app.get('/', function(req, res){ articleProvider.findAll( function(error,docs){ if(error) { console.log("mongo db error"+error); docs = []; } res.render('index.jade', {title: 'Blog', articles:docs}); }) });