1

翡翠を使用して、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

私はすべての依存関係について多くのことを読みましたが、まだそれらに慣れていません。私が収集できるものから、これらのいずれかが問題である可能性があります。私が正しければ、詳細な解決策を教えてください.

  1. index.jade コードが正しくありません

  2. index.jade は配列を参照しており、記事オブジェクトは配列ではありません

  3. mongodb にはアプリへの適切な接続が確立されていません

  4. 私は僧侶を使う必要がありますが、私はそうではありません

私のコードの一部は、この記事 http://howtonode.org/express-mongodbから来ています

前もって感謝します

4

1 に答える 1

1

1.急行ルートを修正する

ルートに複数のrender呼び出しがあります。に変更する必要があります。

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

2.articlesループする前に jade ビューでチェックが定義されている

記事の配列をトラバースする前に、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 クエリでエラー パラメータを処理する

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

    })
});
于 2013-11-09T13:47:30.010 に答える