我正在建设一个nodejs,快递,mongodb博客,与玉。
我的文件夹结构是:项目/模块/视图/ index.jade app.js条款提供者-Memy.js条款提供者-monGodb.js
当我通过控制台运行节点app.js并转到本地主机端口时,我得到了TypeError:
无法读取属性“长度”的未定义在jade.debug.unshift.lineno.
在浏览器里。可能是指匿名函数。
这里是条款提供者-Memy.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我读过很多关于所有依赖项的文章,但对它们来说仍然是个新手。从我所能收集到的任何这些都可能是问题所在,如果我是对的,请告诉我一个详细的补救办法。
我的一些代码来自本文http://howtonode.org/express-mongodb
事先谢谢你
发布于 2013-11-09 13:47:30
1.修定特快路线
您的路线是有多个render呼叫。应该修改为。
app.get('/', function(req, res){
articleProvider.findAll( function(error,docs){
res.render('index.jade', {title: 'Blog', articles:docs});
})
});2.在环化之前,在玉视图中定义了Check articles。
在玉器视图中,在遍历物品数组之前,确保它已经定义好了。
使用
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.body3.处理mongo查询中的错误参数
此外,您还必须考虑回调中可用的error变量。因此,如果在查询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});
})
});https://stackoverflow.com/questions/19876435
复制相似问题