【问题标题】:EJS ERROR: SyntaxError: missing ) after argument list in while compiling ejsEJS ERROR: SyntaxError: missing ) 在编译 ejs 时参数列表后
【发布时间】:2021-05-15 07:03:45
【问题描述】:

我之前使用车把 (hbs),现在我将“hbs”切换为“ejs”。我收到这个错误! 我是 ejs 新手。我正在尝试在仪表板上显示特定的用户文件。 我究竟做错了什么? 请帮帮我!!

错误:

SyntaxError: missing ) after argument list in E:\EXP_NP\ALPHA\views\dashboard.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 (E:\EXP_NP\ALPHA\node_modules\ejs\lib\ejs.js:662:12)
    at Object.compile (E:\EXP_NP\ALPHA\node_modules\ejs\lib\ejs.js:396:16)
    at handleCache (E:\EXP_NP\ALPHA\node_modules\ejs\lib\ejs.js:233:18)
    at tryHandleCache (E:\EXP_NP\ALPHA\node_modules\ejs\lib\ejs.js:272:16)
    at View.exports.renderFile [as engine] (E:\EXP_NP\ALPHA\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (E:\EXP_NP\ALPHA\node_modules\express\lib\view.js:135:8)
    at tryRender (E:\EXP_NP\ALPHA\node_modules\express\lib\application.js:640:10)
    at Function.render (E:\EXP_NP\ALPHA\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (E:\EXP_NP\ALPHA\node_modules\express\lib\response.js:1012:7)
    at ServerResponse.res.render (E:\EXP_NP\ALPHA\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
    at E:\EXP_NP\ALPHA\routes\users.js:43:13
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

仪表板 EJS 代码:(views/dashboard.ejs) dashboard.ejs

<nav class="navbar navbar-expand-lg navbar-light" style="background-color: #222;">
    <a class="navbar-brand text-white hsize" href="#">DASHBOARD</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"        aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav ml-auto">
            <a href="logout" class="btn btn-outline-light btn-md" role="button">LOGOUT</a>
        </ul>
    </div>
</nav>
<div class="container mx-auto">
    <h1>Welcome <%= name %></h1>
        <div class="btable">
            <a href="/files/add" class="btn btn-outline-dark" role="button"><i class="fas fa-plus-circle"></i> Add
                Files</a>
            <input type="text" class="field small-field" style="flex-grow: 0.5;" />
            <input type="submit" class="button btn btn-outline-dark btn-sm" value="Search" />
        </div>
<% if(files) { %>
    <table width="100%">
        <thead>
            <tr>
                <th width="1">TITLE</th>
                <th width="400">CREATED AT</th>
                <th width="120">EDIT FILES</th>
                <th width="100">DELETE FILES</th>
            </tr>
        </thead>
        <tbody>
            <% items.forEach(function(item) { %>
        <tr>
            <td><a href="/files/<%- _id %>"></a><%= title %></td>
            <td><%= formatDate createdAt 'MMMM Do YYYY, h:mm:ss a' %></td>
            <td>
                <a href="/files/edit/<%- _id %>" class="btn btn-outline-dark btn-float">
                    <i class="fas fa-edit"></i>
                </a>
            </td>
            <td>
                <form action="/files/<%- _id %>" method="POST" id="delete-form">
                    <input type="hidden" name="_method" value="DELETE">
                    <button type="submit" class="btn btn-outline-dark">
                         <i class="fas fa-trash "></i>
                    </button>
                </form>
            </td>
        </tr>
            <% }); %>
        </tbody>
    </table>
</div>
<% } else { %>
    <p class="m-2">You have not created any files</p>
    <p class="m-2">Click Add Files to create new files</p>
<% } %>

路由器代码:(routes/user.js) users.js

// @desc    Dashboard
// @route   GET /users/dashboard
router.get('/dashboard', ensureAuth, async (req, res) => {

    try {
        const files = await File.find({user: req.user.id }).lean()

        res.render('dashboard', {
            layout: 'dashboard',
            title: 'Dashboard',
            name: req.user.name,
            files
        })

    } catch (error) {
        console.error(err);
        res.render('error/500')
    }

})

【问题讨论】:

    标签: javascript node.js express handlebars.js ejs


    【解决方案1】:

    问题

    第二个&lt;td&gt;标签

    <td><%= formatDate createdAt 'MMMM Do YYYY, h:mm:ss a' %></td>
    

    没有执行您的 formatDate() 函数并正确传递这些参数。

    潜在解决方案

    formatDate() 需要像在常规 javascript 文件中一样正确执行,并且您还需要从您要映射的每个 item 中引用 _idtitlecreatedAt 值通过。

    下面的代码展示了你应该做什么

    <tbody>
        <% items.forEach(function(item) { %>
    <tr>
        <td><a href="/files/<%- item._id %>"></a><%= item.title %></td>
        <td><%= formatDate(item.createdAt, 'MMMM Do YYYY, h:mm:ss a') %></td>
        <td>
            <a href="/files/edit/<%- item._id %>" class="btn btn-outline-dark btn-float">
                <i class="fas fa-edit"></i>
            </a>
        </td>
        <td>
            <form action="/files/<%- item._id %>" method="POST" id="delete-form">
                <input type="hidden" name="_method" value="DELETE">
                <button type="submit" class="btn btn-outline-dark">
                     <i class="fas fa-trash "></i>
                </button>
            </form>
        </td>
    </tr>
        <% }); %>
    </tbody>
    

    注意:formatDate() 函数应在其范围内可用,否则将导致undefined

    这里有一个ejs playground tool 供进一步修改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多