【问题标题】:jade loop not able to access variable in node.js file玉循环无法访问 node.js 文件中的变量
【发布时间】:2014-09-24 00:26:39
【问题描述】:

我这几天一直在寻找解决方案,希望得到任何解决方案或建议。

根据this how to node tutorial,我正在用 node.js、express 和 mongo db 构建一个非常简单的博客。我这样做是为了更好地理解节点,因为我还不是很精通。

我遵循了所有说明,考虑到了版本的差异,一切都运行良好,直到我到达教程中的标题“A View to Kill”,在那里我将一些虚拟数据发送到要渲染的玉文件。

如果我将代码保留在我的 app.js 文件中,而没有将玉文件传递给它:

var articleProvider= new ArticleProvider();

app.get('/', function(req, res){
  articleProvider.findAll(function(error, docs){
      res.send(docs);
  });
})

app.listen(3000);

我得到了数据对象的渲染。

但是当我传入要渲染的jade文件时,我得到了错误:

TypeError: Cannot read property 'length' of undefined

...指的是我的jade文件(第3行)中的每个循环,它在这里:

h1= title
#articles
    - each article in articles
      div.article
        div.created_at= article.created_at
        div.title 
            a(href="/blog/"+article._id)!= article.title
        div.body= article.body

Here is the link 到整个该死的东西的 git。非常感谢任何帮助!

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    articles 引用未定义。尝试将您的渲染代码更改为以下内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多