尝试从数组中打印字符串时,为什么会出现EJS语法错误?

时间:2018-10-23 13:46:49

标签: javascript node.js express ejs

我正在开发自己的第一个真实Web应用程序,当前正在使用Node,Express和MongoDB进行开发。我数据库中的所有数据都遵循{question: String, learningDate: Date}模式。我想创建一个页面,在相应的日期下将问题打印到该页面。例如,如果两个问题的日期值相同,则它们都应在一个日期和同一日期下打印。为了实现这一目标,我编写了下面的代码。

我在运行应用程序时遇到以下错误:“ SyntaxError:意外令牌;编译ejs时在/path/to/file/views/home.ejs中”

我尝试使用该代码已有一段时间,但无法弄清楚是什么原因引起的?有谁知道如何解决此错误,或者是否有关于如何以不同方式实现所需功能的建议?

app.js https://pastebin.com/bhZdcNzJ

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

const app = express();
app.set("view engine", "ejs");

mongoose.connect("mongodb://localhost/review1");

const reviewSchema = mongoose.Schema(
    {
        question: String,
        learningDate: Date
    }
)

const Review = mongoose.model("Review", reviewSchema);

app.get("/", function(req, res)
{
    Review.find({}, function(err, reviews)
    {
        if (err)
        {
            console.log("Something went wrong.");
            console.log(err);
        }
        else
        {
            res.render("home", {reviews: reviews});
        }
    });
});

app.listen(3000, function()
{
    console.log("Active Recall server is listening on port 3000.")
});

home.ejs https://pastebin.com/e0bN20Yz

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="author" content="Konrad M. L. Claesson"/>
    <meta name="viewport" content="width=device-width, initial-sacle=1"/>
    <title>Active Recall</title>
</head>

<body>
    <h1>Active Recall</h1>

    <%
    const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    const months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"]

    function formatDate(date)
    {
        var day = days[date.getDay()];
        var month = months[date.getMonth()];
        var dd = date.getDate();
        var yyyy = date.getFullYear();
        return day + ", " + dd + " " + month + " " + yyyy;
    }
    %>

    <%
    function getReviewDate(learningDate, reviewNumber)
    {
        const reviewSpace = 2 * (Math.pow(2, reviewNumber) - 1) - reviewNumber;
        let date = new Date();
        date.setDate(learningDate.getDate() + reviewSpace);
        return date;
    }
    %>

    <% function getReviewsByDate(reviews, learningDate) %>
    <% { %>
        <% let reviewsOnDate = []; %>
        <% learningDate = learningDate.valueOf(); %>
        <% reviews.forEach(function(reivew) %>
        <% { %>
            <% if (review.learningDate.valueOf() === learningDate) %>
            <% { %>
                <% reviewsOnDate.push(review); %>
            <% } %>
        <% }); %>
    <% } %>

    <%
    function removeReviewsByDate(reivews, learningDate)
    {
        reviews.forEach(function(reivew)
        {
            if (review.learningDate.valueOf() === learningDate)
            {
                let i = reviews.indexOf(review);
                reviews.splice(i, 1);
            }
        });
    }
    %>

    <% for (let i = reviews.length - 1; i > -1; i--) { %>
        <% let learningDate = reviews[i].learningDate; %>
        <% let reviewsOnDate = getReviewsByDate(reviews, learningDate); %>
        <% removeReviewsByDate(reviews, learningDate); %>

        <% for (let reviewNumber = 1; reviewNumber < 11; reviewNumber++) %>
        <% { %>
            <h4> <%= formatDate(getReviewDate(learningDate, reviewNumber)) %> </h4>
            <% reviewsOnDate.forEach(function(review) %>
            <% { %>
                <li> <%= review.question %> </li>
            <% }); %>
        <% } %>
    <% } %>
</body>
</html>

当我从头开始重写“ home.ejs”中的代码时,我注意到以下代码会产生我的错误:

<% function getReviewsByDate(reviews, learningDate) %>
<% { %>
    <% let reviewsOnDate = []; %>
    <% learningDate = learningDate.valueOf(); %>
    <% reviews.forEach(function(reivew) %>
    <% { %>
        <% if (review.learningDate.valueOf() === learningDate) %>
        <% { %>
            <% reviewsOnDate.push(review); %>
        <% } %>
    <% }); %>
<% } %>

更新1 将摘录的代码从“ home.ejs”重写为以下代码可解决语法错误。

<% 
function getReviewsByDate(reviews, learningDate)
{
    let reviewsOnDate = [];
    learningDate = learningDate.valueOf();
    reviews.forEach(function(reivew)
    {
        if (review.learningDate.valueOf() === learningDate)
        {
            reviewsOnDate.push(review);
        }
    });
} 
%>

但是,当将以下代码重新包含在“ home.ejs”中时,会返回相同的语法错误消息:

<% for (let i = reviews.length - 1; i > -1; i--) { %>
    <% let learningDate = reviews[i].learningDate; %>
    <% let reviewsOnDate = getReviewsByDate(reviews, learningDate); %>
    <% removeReviewsByDate(reviews, learningDate); %>

    <% for (let reviewNumber = 1; reviewNumber < 11; reviewNumber++) %>
    <% { %>
        <h4> <%= formatDate(getReviewDate(learningDate, reviewNumber)) %> </h4>
        <% reviewsOnDate.forEach(function(review) %>
        <% { %>
            <li> <%= review.question %> </li>
        <% }); %>
    <% } %>
<% } %>

1 个答案:

答案 0 :(得分:0)

也许替换

<h4> <%= formatDate(getReviewDate(learningDate, reviewNumber)) %> </h4>

使用

<h4> <%- formatDate(getReviewDate(learningDate, reviewNumber)) %> </h4>