繁体   English   中英

SyntaxError: 意外的标记 ';' 在<file path>在编译 ejs 时</file>

[英]SyntaxError: Unexpected token ';' in <file path> while compiling ejs

每当我重新加载我的网站时,我都会收到此错误:

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

这是我的 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("服务器在 3000 端口启动"); });

这是我的 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 告诉我这个:

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

我不知道如何修复它,我已经删除了所有的“;” 那是在我的 home.ejs 文件中,我也尝试使用普通的 function,Ejslint 至少对我来说不是很有帮助。

你有两个括号删除一个

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

改成这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM