Home > Enterprise >  Express And Ejs Unexpected identifier
Express And Ejs Unexpected identifier

Time:12-03

** I came across this error and tried to solve it using the git... But i cant really find out the real solution to to problem.. Is it the EJS template or form the app. Creating a To Do list app from web development app... I also tried installing ejs-lint to see the problem...But that also gave it own error**

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);
});

CodePudding user response:

The error message you are seeing indicates that there is a syntax error in your list.ejs file. In particular, it looks like there is an unexpected identifier (i.e. a character that is not valid in JavaScript) on the line that contains only three backtick characters (```).

To fix this error, you can either remove this line entirely, or replace the backtick characters with a string or code that is valid in JavaScript. For example, you could replace the line with the following code:

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

This code uses an if statement to check if the value of the kindOfDay variable is "Saturday" or "Sunday". If it is, the h1 element is given a red color, and if not, it is given a blue color. This code should fix the syntax error and allow your app to run without any problems.

  • Related