1

whenever I reload my website I get this error:

SyntaxError: Unexpected token ';' in /Users/<name>/Desktop/Web_Development/ejs-challenge/views/home.ejs while compiling ejs

this is my app.js:

//jshint esversion:6

const express = require("express");
const ejs = require("ejs");

const homeStartingContent = "<placeholder text>";
const contactContent = "<placeholder text>";
const aboutContent = "<placeholder text>";

const app = express();

let posts = [];

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

app.use(express.json());

app.use(express.urlencoded({extended: true}));

app.use(express.static("public"));

app.get("/", function(req, res){
  res.render("home.ejs", {homeContent: homeStartingContent, 
  posts: posts});
});

app.get("/about", function(req, res){
  res.render("about.ejs", {aboutContent: aboutContent,});
});

app.get("/contact", function(req, res){
  res.render("contact.ejs", {contactContent: contactContent,});
});

app.get("/compose", function(req, res){
  res.render("compose.ejs");
});

app.post("/compose", function(req, res){

  const post = {
    title: req.body.postTitle,
    body: req.body.postBody,
  };

  posts.push(post);

  res.redirect("/");
});

app.listen(3000, function() {
  consol

e.log("Server started on port 3000"); });

this is my ejs:

<%- include('partials/header') -%>
<h1>Home</h1>
<p><%= homeContent %> </p>

<% posts.forEach(post => %>
    <h1> <%= post.title %> </h1>
    <p>  <%= post.body %> </p>
<% )) %>

<%- include('partials/footer') -%>

ejslint tells me this:

Unexpected token (6:9) in home.ejs
<h1><%= post.title %></h1>
    ^    

I don't know how to fix it, I have already removed all of the ";" that was in my home.ejs file, I have tried to use a normal function as well, Ejslint is not very helpful at least for me .

1 Answer 1

2

you have two enclosing brackets remove one

<% posts.forEach(post => %>
    <h1> <%= post.title %> </h1>
    <p>  <%= post.body %> </p>
<% )) %>

change to this

<% posts.forEach(post => %>
        <h1> <%= post.title %> </h1>
        <p>  <%= post.body %> </p>
    <% ) %>
Sign up to request clarification or add additional context in comments.

2 Comments

i changed to <% posts.forEach(post => %> <h1> <%= post.title %> </h1> <p> <%= post.body %> </p> <% ) %> still getting the same error though, even restarted my server.
you forgot adding curly brackets after the arrow <% posts.forEach(post =>{ %> <h1> <%= post.title %> </h1> <p> <%= post.body %> </p> <%} ) %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.