NODE.JS / EJS-ReferenceError:file.ejs无法识别变量的定义

时间:2018-07-07 03:04:53

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

我对我的express.js服务器的模板有问题。我收到“未定义x”的错误,但实际上我看不到我在搞乱什么。 Cross检查了一些正在使用同一教程的朋友代码,并且在其计算机上可以正常工作。

FILE.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)

1 个答案:

答案 0 :(得分:0)

您的ejs文件错误,您已经编写了玉模板的标签

<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>

尝试此代码。