![](/img/trans.png)
[英]How are these two lines of code different that one throws an error and the other doesn’t?
[英]EJS partial that doesn't have problems on other pages throws 500 error on one page
我有一個頁腳部分:
</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
object 中的其他項目沒有任何問題:
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')
只有在定義頁面時才會出現錯誤,這就是其他頁面正確加載的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.