Javascript mongodb articles.length未定义

Javascript mongodb articles.length未定义,javascript,node.js,mongodb,express,pug,Javascript,Node.js,Mongodb,Express,Pug,我正在用jade建立一个nodejs、express、mongodb博客 我的文件夹结构是: 计划/ 模块/ 观点/ 翡翠索引 app.js articleprovider-memory.js articleprovider-mongodb.js ArticleProvider = function(host, port) { this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {

我正在用jade建立一个nodejs、express、mongodb博客

我的文件夹结构是: 计划/ 模块/ 观点/ 翡翠索引 app.js articleprovider-memory.js 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;
当我通过控制台运行node app.js并转到localhost端口时,我得到TypeError:

无法读取jade.debug.unshift.lineno处未定义的属性“length”

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

这是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;
然后是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引用的是数组,而my articles对象不是数组

  • mongodb没有与应用程序建立正确的连接

  • 我需要用monk,但我不是

  • 我的一些代码来自本文

    提前谢谢你

    1。修快线 您的路由正在进行多个
    render
    调用。它应该修改为

    app.get('/', function(req, res){    
        articleProvider.findAll( function(error,docs){        
            res.render('index.jade', {title: 'Blog', articles:docs});
        })
    });
    
    2.在循环之前,检查jade视图中是否定义了
    文章
    在遍历articles数组之前,在jade视图中,确保已经定义了它

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

    您还考虑了回调中可用的<代码>错误<代码>变量。因此,如果在查询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});
    
        })
    });
    
    1.修快线 您的路由正在进行多个
    render
    调用。它应该修改为

    app.get('/', function(req, res){    
        articleProvider.findAll( function(error,docs){        
            res.render('index.jade', {title: 'Blog', articles:docs});
        })
    });
    
    2.在循环之前,检查jade视图中是否定义了
    文章
    在遍历articles数组之前,在jade视图中,确保已经定义了它

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

    您还考虑了回调中可用的<代码>错误<代码>变量。因此,如果在查询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});
    
        })
    });
    

    为什么在路线中渲染index.jade两次?为什么在路线中渲染index.jade两次?谢谢。当我接近我的代码的最终编译时,我在错误检查方面没有那么小心。谢谢。当我接近我的代码的最终编译时,我在错误检查方面没有那么小心。