【问题标题】:how to resolve this Error: Could not find matching close tag for "<%" [closed]如何解决此错误:找不到“<%”的匹配关闭标记 [关闭]
【发布时间】:2021-04-21 21:14:03
【问题描述】:

我正在尝试使用 html 和 nodejs 构建网页。我在html代码的某些地方使用了ejs。运行服务器时出现以下错误。虽然我已经为每个“

Error: Could not find matching close tag for "<%".
    at C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:739:19
    at Array.forEach (<anonymous>)
    at Template.generateSource (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:729:15)
    at Template.compile (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:583:12)
    at Object.compile (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:396:16)
    at handleCache (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:233:18)
    at tryHandleCache (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:272:16)
    at View.exports.renderFile [as engine] (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\node_modules\express\lib\response.js:1012:7)
    at C:\Users\GANDHARAV\OneDrive\Desktop\nodejs\first_app\server.js:19:7
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

以下是我的html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <meta http-equiv="X-UA-Comaptible" content="ie=edge">
    <title>Blog</title>
</head>
<body>
    <div class="container">
        <h1 class="mb-4">Blog Articles</h1>
        <a href="/articles/new" class="btn btn-succeess">New Article</a>

        <% articles.forEach(article = > { %>
            <div class="card mt-4">
                <div class="card-body">
                    <h4 class="card-title"><%= article.title %></h4>
                    <div class="card-subtitle tetxt-muted mb-2">
                        <%= article.createdAt.toLocalDateString() %>
                    </div>
                    <div class="card-text mb-2"><%= article.description %></div>
                    <a href="articles/<%= article.slug %>" class="btn btn-primary"> Read more </a>
                    <a href="articles/edit/<%= article.id %>" class="btn btn-info"> Edit </a>
                    <form action="/articles/<%= article.id %>?_method=DELETE" method="POST" class="d-inline">
                        <button type="submit" class="btn btn-danger">Delete</button>
                    </form>
                </div>
            </div>
        <% }) >
    </div>
</body>
</html>

**我已经安装了以下依赖项:**

{
  "name": "first_app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dompurify": "^2.2.6",
    "ejs": "^3.1.5",
    "express": "^4.17.1",
    "jsdom": "^16.4.0",
    "marked": "^1.2.7",
    "method-override": "^3.0.0",
    "mongoose": "^5.11.12",
    "nodemon": "^2.0.7",
    "slugify": "^1.4.6"
  }
}

从下图中,VS 代码似乎无法识别 ejs 代码。因为无论我从 "

【问题讨论】:

  • 这个问题应该被关闭,因为它是由一个错字引起的。

标签: javascript html node.js express ejs


【解决方案1】:

你的 %&gt; 结束标签在 foreach 循环的结束标签上丢失,它应该是 &lt;% }) %&gt; 你有 &lt;% }) &gt;

foreach 循环顶部的=&gt; 之间还有一个空格&lt;% articles.forEach(article = &gt; { %&gt;,这可能会导致问题。尝试将其更改为&lt;% articles.forEach(article =&gt; { %&gt;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <meta http-equiv="X-UA-Comaptible" content="ie=edge">
    <title>Blog</title>
</head>
<body>
    <div class="container">
        <h1 class="mb-4">Blog Articles</h1>
        <a href="/articles/new" class="btn btn-succeess">New Article</a>

        <% articles.forEach(article => { %>
            <div class="card mt-4">
                <div class="card-body">
                    <h4 class="card-title"><%= article.title %></h4>
                    <div class="card-subtitle tetxt-muted mb-2">
                        <%= article.createdAt.toLocalDateString() %>
                    </div>
                    <div class="card-text mb-2"><%= article.description %></div>
                    <a href="articles/<%= article.slug %>" class="btn btn-primary"> Read more </a>
                    <a href="articles/edit/<%= article.id %>" class="btn btn-info"> Edit </a>
                    <form action="/articles/<%= article.id %>?_method=DELETE" method="POST" class="d-inline">
                        <button type="submit" class="btn btn-danger">Delete</button>
                    </form>
                </div>
            </div>
        <% }) %>
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2017-05-01
    • 2021-10-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多