在其他页面上没有问题的 EJS 部分在一个页面上抛出 500 错误



我有一个页脚部分:

</main>
<footer class="pt-3 container">
<p id="copyright" class="">
Copyright 2020 Robert Greenstreet
</p>
<!-- <script src="/javascripts/jquery-ui.min.js" ></script> -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="/javascripts/script.js" type="text/javascript"></script>
<% if(page && == 'ownerDashboard') { %>
<script src="/javascripts/canvasRender.js"></script>
<% } %>
<script>
</script>
</footer>
</body>
</html>

它在其他页面上加载良好,但在这个特定页面上,只有当我注释掉那个小的if块时,它才会加载。这就像我还没有传入page变量一样。但即使我没有,它应该仍然可以工作,因为&&运算符,对吧?

这是页面EJS文件:

<%- include("../partials/header") %>
<div class="container mt-5">
<h1>
<%= currentCompany.name %>
</h1>
<div class="row shadow-md">
<div class="col">
<table class="table">
<!-- <thead>
<tr>
<th scope="col" class="border-0 bg-light"></th>
</tr>
</thead> -->
<tbody>
<tr>
<td class="border-0 align-middle">
Locations
</td>
<td class="border-0 align-middle">
<%= currentCompany.locations.length %>
</td>
</tr>
<tr>
<td class="border-0 align-middle">
Total Admins
</td>
<td class="border-0 align-middle">
<% let totalAdmins = 0 %>
<% for (let location of currentCompany.locations) { %>
<% totalAdmins += location.contacts.length %>
<% } %>
<%= totalAdmins %>
</td>
</tr>
<tr>
<td class="border-0 align-middle">
Total Users
</td>
<td class="border-0 align-middle">
<%= userCount %>
</td>
</tr>
<tr>
<td class="border-0 align-middle">
Total Forms
</td>
<td class="border-0 align-middle">
<% let totalForms = 0 %>
<% for (let location of currentCompany.locations) { %>
<% totalForms += location.contacts.length %>
<% } %>
<%= totalForms %>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script>
</script>
<%- include("../partials/footer") %>

所有这些加载都很好,只是页脚部分

以下是节点render调用(假设data对象中的其他项没有任何问题:

res.render('../views/company/profile.ejs', {currentCompany, userCount, page: 'companyProfile', title: 'Company Profile'})

你知道为什么这会给我带来麻烦吗?就像我说的,它在任何其他页面上都很好用。我试过ejs-lint,但我不知道如何让它真正向我展示任何有用的东西。他们的NPM自述毫无用处。

此代码的第二部分出现错误:

if (page && == 'ownerDashboard')

应该是

if (page && someVariable == 'ownerDashboard')

或者如果是这样的话:

if (page && page == 'ownerDashboard')

只有在定义页面时才会出现错误,这就是其他页面正确加载的原因。

相关内容

最新更新