首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb articles.length未定义

mongodb articles.length未定义
EN

Stack Overflow用户
提问于 2013-11-09 13:21:16
回答 1查看 2.5K关注 0票数 1

我正在建设一个nodejs,快递,mongodb博客,与玉。

我的文件夹结构是:项目/模块/视图/ index.jade app.js条款提供者-Memy.js条款提供者-monGodb.js

当我通过控制台运行节点app.js并转到本地主机端口时,我得到了TypeError:

无法读取属性“长度”的未定义在jade.debug.unshift.lineno.

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

这里是条款提供者-Memy.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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;

这是我的路线:

代码语言:javascript
复制
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文件

代码语言:javascript
复制
// 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

事先谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-09 13:47:30

1.修定特快路线

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

代码语言:javascript
复制
app.get('/', function(req, res){    
    articleProvider.findAll( function(error,docs){        
        res.render('index.jade', {title: 'Blog', articles:docs});
    })
});

2.在环化之前,在玉视图中定义了Check articles

在玉器视图中,在遍历物品数组之前,确保它已经定义好了。

使用

代码语言:javascript
复制
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时发生了任何错误,则可以进行处理。喜欢

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19876435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档