【问题标题】:NODE.JS / EJS - ReferenceError: file.ejs can't recognize definition of variablesNODE.JS / EJS - ReferenceError:file.ejs 无法识别变量的定义
【发布时间】:2018-12-15 14:29:39
【问题描述】:

我对我的 express.js 服务器的模板方式有疑问。我收到“未定义 x”的错误,但实际上我看不到任何地方我搞砸了。交叉检查了一些正在使用相同教程的朋友的代码,并且在他的机器上它可以工作。

文件.EJS:

<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        {% for (var x in cats) { %}
            <a href=<%= cats[x].href %>><%= cats[x].name%></a>
        {% } %}
    </body>
</html>

SERVER.JS:

var express = require("express");
var app = express();

app.use(express.static(__dirname + '/static'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs')

app.get("/", function (request, response) {
    response.render("cats")
})
app.get("/cats", function (request, response) {
    var kitty_arr = [
        {name: "Zues", age: "2", href: "/cats/zues"},
        {name: "Scarlet", age: "3", href: "/cats/scarlet"},
        {name: "Saphire", age: "1", href: "/cats/saphire"},
    ];
    response.render("cats", {cats: kitty_arr})
})
app.get("/cats/zues", function (request, response) {
    console.log('hit zues')
    var kitty_arr = [
        {name: "Zues", age: "2", hobbies: "Head big boss of catnip cartel, loves the finer things in life", favetoys: ['catnip', 'whiskey', 'gold mc-hammer pants']},
    ]
    response.render("cats", {kitties: kitty_arr})
})
app.get("/cats/scarlet", function (request, response) {
    console.log('hit scarlet')
    var kitty_arr = [
        {name: "Scarlet", age: "3", hobbies: "Misunderstood weird artist, loves to hang in the background and paint", favetoys: ['catnip', 'whiskey', 'gold mc-hammer pants']},
    ]
    response.render("cats", {kitties: kitty_arr})
})
app.get("/cats/saphire", function (request, response) {
    console.log('hit saphire')
    var kitty_arr = [
        {name: "Saphire", age: "1", hobbies: "Chunky little thing that has no talents other than running real slow and cuddling", favetoys: ['catnip', 'whiskey', 'gold mc-hammer pants']},
    ]
    response.render("cats", {kitties: kitty_arr})
})
app.listen(8000, function() {
    console.log('yatzi')
})

追踪:

ReferenceError: /Users/a666/Desktop/Development/MEAN/Express.js/catsData/views/cats.ejs:8
    6|     <body>
    7|         {% for (var x in cats) { %}
 >> 8|             <a href=<%= cats[x].href %>><%= cats[x].name%></a>
    9|         {% } %}
    10|     </body>
    11| </html>

x is not defined
    at eval (eval at compile (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:11:31)
    at returnedFn (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/ejs/lib/ejs.js:653:17)
    at tryHandleCache (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/ejs/lib/ejs.js:251:36)
    at View.exports.renderFile [as engine] (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/ejs/lib/ejs.js:482:10)
    at View.render (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/express/lib/application.js:640:10)
    at EventEmitter.render (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/express/lib/response.js:1008:7)
    at /Users/a666/Desktop/Development/MEAN/Express.js/catsData/server.js:17:14
    at Layer.handle [as handle_request] (/Users/a666/Desktop/Development/MEAN/Express.js/catsData/node_modules/express/lib/router/layer.js:95:5)

【问题讨论】:

    标签: javascript node.js express ejs embedded-javascript


    【解决方案1】:

    你的ejs文件错误,你写了jade模板的标签

    <html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <% for (var x=0;x<cats.length;x++) { %>
            <a href="<%= cats[x].href %>"><%= cats[x].name %></a>
        <% } %>
    </body>
    

    试试这个代码。

    【讨论】:

    • 奇怪,这就是我朋友的性格。仍然不适用于单个刻度?
    • 在将 {% 更改为
    • 是的,现在错误已经转移到与猫有关的参考错误:'cats is not defined',cats.ejs 中的第 7 行。
    • 尝试 在 body 标签内,看看它是否在该行工作或抛出错误
    • 没想到。好决定。是的,这就是奇怪的和我怀疑的。它以这种方式工作,它在数组中显示猫对象,即使我这样做&lt;%= cats[0].href %&gt; 它也会拉起位于第零位的对象中的“href”键。由于某种原因,它没有进入循环范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多