【问题标题】:ReferenceError: jade is not definedReferenceError: 玉未定义
【发布时间】:2013-09-27 10:51:03
【问题描述】:

我一直在尝试使用express和jade创建一个网站。我已经制作了几个页面和路线,到目前为止它运行良好。我不断收到以下错误:

ReferenceError: jade is not defined
    at eval (eval at <anonymous> ([...]project/node_modules/jade/lib/jade.js:165:30), <anonymous>:2:1)
    at Object.exports.render ([...]project/node_modules/jade/lib/jade.js:211:10)
    at Object.exports.renderFile ([...]project/node_modules/jade/lib/jade.js:247:18)
    at View.exports.renderFile [as engine] ([...]project/node_modules/jade/lib/jade.js:232:21)
    at View.render ([...]project/node_modules/express/lib/view.js:76:8)
    at Function.app.render ([...]project/node_modules/express/lib/application.js:506:10)
    at ServerResponse.res.render (/media/ruben/5CCAD622CAD5F7EA/Users/ruben/Google Drev/developer/workspace/kwasi/project/node_modules/express/lib/response.js:759:7)
    at exports.home ([...]project/routes/cases.js:7:7)
    at callbacks ([...]project/node_modules/express/lib/router/index.js:164:37)
    at clientChosen ([...]project/app.js:81:5)

当我清除缓存和 cookie 时,我可以访问我的所有页面,但是每当我登录时,即使用已登录的用户设置会话变量时,都会出现上述错误。

我不确定我还可以包含哪些其他信息。堆栈跟踪引用的 case.js 中的行很简单

res.render("home.jade", {})

编辑:

在我的 app.js 中,我使用以下内容指定了jade 作为渲染引擎:

app.configure(function() {
  app.set("views", __dirname + "/views"); // Set the views folder
  app.set("view engine", "jade"); // Set the rendering engine
  ...

我的观点是这样的:

extend layout

block title
  title Forside - #{company}

block content
  h3 Sager

  - for (var i = 0; i < cases.length; i++) {
    .casethumb
      p #{cases[i].label}
  - }

编辑 2: 正确的。我能够确定的是它可能与我的自定义中间件有关。我有这两个功能

// Makes sure that the user is logged in
loggedIn = function(req, res, next){
  if (req.session.user) {
    next()
  }
  else {
    if (req.url != "/login") {
      req.flash('info', "Før du kan bruge systemet, skal du logge ind")
    }
    res.redirect("/login")
  }
}
// Makes sure that the user has chosen a client to work on
clientChosen = function(req, res, next) {
  if (req.session.selectedClient) {
    next()
  }
  else {
    res.redirect("/chooseClient")
  }
}

我是这样使用它们的:

app.get("/", loggedIn, clientChosen, cases.home)

当我像这样从我的路线中删除该功能时

app.get("/", cases.home)

问题消失了,但我确实需要中间件的功能。我该如何解决这个问题?

【问题讨论】:

  • 我已经编辑了我的问题以包括我的玉配置和有问题的视图
  • 这似乎与会话变量的存储方式有关。我会进一步调查
  • 你能粘贴cases.home()代码吗?你的另外两个 m/ws 在我看来很好。

标签: node.js session express pug


【解决方案1】:

另外,你能在loggedIn()clientChosen() 例程定义之前var 吗?在使用这些定义之前放置这些定义app.get('/');

var loggedIn = function (req, res, next) { // your code };

var clientChosen = function (req, res, next) { // your code };

如果你还没有使用它,你可以安装(npm install -g jshint)并运行jshint &lt;path/to/js-file&gt;吗?它是否报告任何错误?

【讨论】:

    【解决方案2】:

    经过一番挖掘,我发现错误与玉有关。使用我使用的翡翠版本,当我尝试require(./something) 时,翡翠源代码中引用了一个不存在的变量。为了解决这个问题,我在./node_modules/jade/lib/jade.js:151中编辑了一行

    来自

    if (options.client) return new Function('locals', fn)
    

    //if (options.client) return new Function('locals', fn)
    

    这条线似乎根本不需要。没有它,一切都可以正常工作。 Jade 的 github 页面曾经提到过这个问题,我给了他们提示。这似乎是一个已知问题,他们正在即将发布的版本中处理。

    对于有相同问题的任何人,只需将这一行注释掉,你就可以了

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 2015-10-04
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多