【问题标题】:Reference error express/Not Defined参考错误表达/未定义
【发布时间】:2014-05-24 08:10:13
【问题描述】:

http://howtonode.org/express-mongodb 我正在尝试运行此示例,当我尝试运行 node app.js 时出现 ReferenceError。

这是错误: 500 参考错误:/home/action/blog/views/index.jade:4 2| h1=标题3| #文章 > 4| - 第 5 条中的每篇文章| div.第 6 条| div.created_at=article.created_at 7| div.title 标题未定义

2| h1= title
3| #articles
4| - each article in articles
5| div.article
6| div.created_at= article.created_at
7| div.title
title is not defined
at eval (eval at (/home/action/blog/node_modules/jade/lib/jade.js:176:8), :19:15)
at /home/action/blog/node_modules/jade/lib/jade.js:181:12
at Object.exports.render (/home/action/blog/node_modules/jade/lib/jade.js:216:14)
at View.exports.renderFile [as engine]             (/home/action/blog/node_modules/jade/lib/jade.js:243:13)
at View.render (/home/action/blog/node_modules/express/lib/view.js:75:8)
at Function.app.render (/home/action/blog/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/home/action/blog/node_modules/express/lib/response.js:753:7)
at /home/action/blog/app.js:38:13
at /home/action/blog/articleprovider-mongodb.js:43:16
at /home/action/blog/node_modules/mongodb/lib/mongodb/cursor.js:159:16

它在 index.jade 文件中,但我似乎无法弄清楚哪里出了问题。

编辑添加: 这是 index.jade 文件:

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

        extends layout 

这是索引页面的 app.get:

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

它在函数中传递标题,所以我真的不确定问题是什么。

【问题讨论】:

  • 发布您应该匹配的 javascript 代码:howtonode.org/express-mongodb/2/app.js。您需要确保在调用 res.render 时传递模板期望的“本地”,包括应该是文章标题的“标题”。
  • 我的 app.js 就是那个;为了确定,我复制了它。 res.render 看起来像这样: res.render('index.jade', { locals: { title: 'Blog',articles:docs

标签: node.js mongodb express referenceerror


【解决方案1】:

好的,所以我认为 express render API 随着时间的推移发生了变化。尝试删除“locals”属性并直接传递数据:

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

【讨论】:

  • 好的,删除了本地人,现在我得到了:500 ReferenceError: /home/action/blog/views/index.jade:6 4| h1=标题5| #文章 > 6| - 第 7 条中的每篇文章| div.第 8 条| div.created_at=article.created_at 9| div.title = article.title 正文未定义
  • 我正在将我拥有的内容与另一个 app.js 和 index.jade 文件进行比较,它们似乎是相同的。当然,我本可以长时间地观察它们,使它们看起来都一样,这是完全可能的
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2021-07-15
  • 2015-09-29
相关资源
最近更新 更多