ReferenceError:未定义路径:EJS

时间:2019-02-26 03:27:22

标签: node.js express model-view-controller

该应用程序可用于商店和管理路线,但不适用于错误路线。错误路线也采用MVC格式,但我也在没有架构的情况下对其进行了测试。
下面是根目录中的app.js片段。

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const errorController = require('./controllers/error');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/admin', adminRoutes);
app.use(shopRoutes);

app.use((req, res, next) => {
  res.status(404).render('404', { pageTitle: 'Page Not Found' });
});

app.listen(3000);

404 EJS代码是

<%- include('includes/head.ejs') %>
</head>

<body>
    <%- include('includes/navigation.ejs') %>
    <h1>Page Not Found!</h1>

<%- include('includes/end.ejs') %>

而且,Navigation.ejs是

<header class="main-header">
    <nav class="main-header__nav">
        <ul class="main-header__item-list">
            <li class="main-header__item">
                <a class="<%= path === '/' ? 'active' : '' %>" href="/">Shop</a>
            </li>
            <li class="main-header__item">
                <a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product">Add Product</a>
            </li>
        </ul>
    </nav>
</header>

它显示以下错误:

ReferenceError: C:\Users\Najus\Desktop\New folder\views\404.ejs:5
    3| 
    4| <body>
 >> 5|     <%- include('includes/navigation.ejs') %>
    6|     <h1>Page Not Found!</h1>
    7| 
    8| <%- include('includes/end.ejs') %>

C:\Users\Najus\Desktop\New folder\views\includes\navigation.ejs:6
    4|         <ul class="main-header__item-list">
    5|             <li class="main-header__item">
 >> 6|                 <a class="<%= path === '/' ? 'active' : '' %>" href="/">Shop</a>
    7|             </li>
    8|             <li class="main-header__item">
    9|                 <a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product">Add Product</a>

path is not defined
    at eval (eval at compile (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:11:26)
    at returnedFn (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:653:17)
    at include (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:651:39)
    at eval (eval at compile (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:12:17)
    at returnedFn (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:653:17)
    at tryHandleCache (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\Najus\Desktop\New folder\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (C:\Users\Najus\Desktop\New folder\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\Najus\Desktop\New folder\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\Najus\Desktop\New folder\node_modules\express\lib\application.js:592:3)

我尝试了各种方式来查找互联网,却找不到调整代码的方式。

2 个答案:

答案 0 :(得分:1)

原因是路径未定义,因此在navigation.ejs中找不到路径的值,因此要解决该问题,应将其添加到render函数中

不久,请尝试在此处的函数render中添加“ path”参数:

.topbar span.call {
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    letter-spacing: 1px;
    white-space:nowrap;
}

变得像这样

res.status(404).render('404', { pageTitle: 'Page Not Found' });

应该可以使它起作用!

答案 1 :(得分:0)

Ahmed Mokhtar提供的解决方案是正确的。我想描述一下。 实际上,当要导航到404 ejs文件时,它将找到“ 路径”。 但是由于您没有为 404 ejs 提供任何对象属性作为 path ,因此抛出了未定义的错误,因为 path 未知到 404 ejs

导航ejs