【问题标题】:mongodb articles.length undefinedmongodb文章.长度未定义
【发布时间】:2013-11-09 13:21:16
【问题描述】:

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

我的文件夹结构是: 项目/ 模块/ 意见/ 索引.jade 应用程序.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;

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

提前谢谢你

【问题讨论】:

  • 你为什么在你的路由中渲染 index.jade 两次?

标签: javascript node.js mongodb express pug


【解决方案1】:

1。修复快速路线

您的路线有多个render 呼叫。应该修改为。

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

2。循环前检查articles是否在jade view中定义

在遍历文章数组之前的玉视图中,确保它已经被定义。

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参数

您还考虑了回调中可用的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});

    })
});

【讨论】:

  • 谢谢。当我接近我的代码的最终编译时,我的错误检查没有那么小心。
猜你喜欢
  • 1970-01-01
  • 2016-01-30
  • 2017-10-28
  • 2012-10-29
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多