NodeJS Express和Ejs意外标识符

bf1o4zei  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(194)

我遇到了这个错误,并试图解决它使用的git...但我不能真正找到真正的解决方案的问题...是它的EJS模板或形式的应用程序。创建一个待办事项列表应用程序从web开发应用程序...我也尝试安装ejs-lint看到这个问题...但也给它自己的错误

SyntaxError: Unexpected identifier in /home/abuhavictor/Documents/programking/EJS/todolist-v1/views/list.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
    at new Function (<anonymous>)
    at Template.compile (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/ejs/lib/ejs.js:673:12)
    at Object.compile (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/ejs/lib/ejs.js:398:16)
    at handleCache (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/ejs/lib/ejs.js:235:18)
    at tryHandleCache (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/ejs/lib/ejs.js:274:16)
    at View.exports.renderFile [as engine] (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/ejs/lib/ejs.js:491:10)
    at View.render (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/express/lib/application.js:657:10)
    at Function.render (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/express/lib/application.js:609:3)
    at ServerResponse.render (/home/abuhavictor/Documents/programking/EJS/todolist-v1/node_modules/express/lib/response.js:1039:7)

list.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>TO DO LIST</title>
  </head>
  <body>
    <% if (kindOfDay === "Saturday" || kindOfDay === "Sunday"){ %>
        <h1 style="color: red"><%= kindOfDay %> ToDo list</h1>
    <% }else { %>
        <h1 style="color: blue"><%= kindOfDay %> ToDo list</h1>
    <% } %>

  </body>
</html>

app.js

const express = require('express');
const bodyparser = require('body-parser');
const port = 5000

const app = express();

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

app.get("/", function (req, res) {

    var today = new Date();
    var currentDay = today.getDay();
    var day = "";

    switch (currentDay) {
        case 0:
            day = 'Sunday';
            break;
        case 1:
            day = 'Monday';
            break;
        case 2:
            day = 'Tuesday';
            break;
        case 3:
            day = 'Wednesday';
            break;
        case 4:
            day = 'Thursday';
            break;
        case 5:
            day = 'Friday';
            break;
        case 6:
            day = 'Saturday';
            break;

        default:
        console.log("Error: Current day is equals to " + currentDay);
            break;
    }

    res.render('list', { kindOfDay: day });
});

app.listen(port, function () {
    console.log('server started on port ' + port);
});
7dl7o3gd

7dl7o3gd1#

您所看到的错误信息表明您的list.ejs文件中存在语法错误。特别是,在只包含三个反勾号字符("')的行中似乎存在一个意外的标识符(即在JavaScript中无效的字符)。
若要修正此错误,您可以完全移除此行,或以JavaScript中有效的字串或程式码取代反勾号字符。例如,您可以以下列程式码取代此行:

<% if (kindOfDay === "Saturday" || kindOfDay === "Sunday"){ %>
        <h1 style="color: red"><%= kindOfDay %> ToDo list</h1>
    <% }else { %>
        <h1 style="color: blue"><%= kindOfDay %> ToDo list</h1>
    <% } %>

此代码使用if语句检查kindOfDay变量的值是否为“Saturday”或“Sunday”。如果是,h1元素将被赋予红色,如果不是,则被赋予蓝色。此代码应修复语法错误,并允许您的应用运行而不出现任何问题。

相关问题