翡翠を使用して、nodejs、express、mongodb ブログを作成しています。
私のフォルダー構造は次のとおりです。 project/ modules/ views/ index.jade app.js articleprovider-memory.js articleprovider-mongodb.js
コンソールから node 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;
記事プロバイダー-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
私はすべての依存関係について多くのことを読みましたが、まだそれらに慣れていません。私が収集できるものから、これらのいずれかが問題である可能性があります。私が正しければ、詳細な解決策を教えてください.
index.jade コードが正しくありません
index.jade は配列を参照しており、記事オブジェクトは配列ではありません
mongodb にはアプリへの適切な接続が確立されていません
私は僧侶を使う必要がありますが、私はそうではありません
私のコードの一部は、この記事 http://howtonode.org/express-mongodbから来ています
前もって感謝します