即使正确渲染,EJS变量也会引发错误 [英] EJS Variable Throws Error Even Though It Renders Properly

查看:67
本文介绍了即使正确渲染,EJS变量也会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ejs模板中迭代访问一系列对象,但是我不断收到参考错误:未定义帐单。

I have an array of objects I am trying to iterate through in an ejs template, but I keep getting "Reference Error: bills is not defined".

其他说说StackOverflow问题来测试变量是否确实定义,所以我用

Other StackOverflow questions said to test whether or not the variable was actually defined, so I used

<h1><%= bills.length %></h1>

这可以正确渲染数组的长度,但仍然会在

This properly renders the length of the array, but it still throws an error at

ReferenceError: /index.ejs:11
    9|     <div id="billCarousel" class="carousel slide" data-ride="carousel">
    10|       <!--Indicators-->
 >> 11|       <h1><%= bills.length %></h1>

如果我将此行替换为

<%= (typeof bills === 'undefined') ? "ERROR" : bills[0].TrackNum %>

然后正确渲染第一个对象的TrackNum,但错误移至

Then the TrackNum of the first object is properly rendered, but the error moves to

ReferenceError: index.ejs:13
    11|       <ol class="carousel-indicators">
    12|         <%= (typeof bills === 'undefined') ? "ERROR" : bills[0].TrackNum %>
 >> 13|         <% for (var i=0; i < bills.length; i++) {%>

您可以在下面看到所有相关代码。

You can see all of the relevant code below.

pages / index.ejs

pages/index.ejs

<ol class="carousel-indicators">
    <h1><%= (typeof bills == 'undefined') ? "ERROR" : bills[0].TrackNum %></h1>
    <% for (var i=0; i < bills.length; i++) {%>
      <li data-target="#billCarousel" data-slide-to="<%= i %>" class="<%= (i == 0) ? 'active':'' %>"></li>
    <% } %>
</ol>

server.js

server.js

app.get('/', function(request, response) {
    billQuery.summary(function(data) {
        console.log(data.bills);
        response.render('pages/index', {
            page: "index",
            status: data.status,
            message: data.message,
            bills: data.bills
        });
    })
});

console.log(data.bills)将数组正确打印到控制台。我尝试在index.ejs中呈现状态和消息,并且它们工作正常。我已经使用

console.log(data.bills) properly prints the array to the console. I have tried rendering status and message in index.ejs, and they work fine. I have already set my Express Engine to EJS with

app.set('view engine', 'ejs');

,在我具有渲染index.ejs的路由之前。我还重新启动了服务器,并多次清除了浏览器的缓存,但是错误不断发生。还有其他我想念的东西吗?

before I have the route to render index.ejs. I have also restarted my server and cleared my browser's cache several times, but the error keeps occurring. Is there something else I am missing?

推荐答案

问题是由于网站尝试加载网站时出现404错误而导致的

The issue was because of a 404 error that resulted when the site tried to load the favicon.

app.get('*', function(request, response) {
    console.log("Couldn't retrieve "+request.url);
    response.render('pages/index', {
        page: "index",
        status: "Failed",
        message: "Error 404"
    });
});

当服务器尝试加载favicon.ico时,它调用了这条路线,这似乎删除了账单我在 /路由中传递的数组。我把

When the server attempted to load favicon.ico, it called this route, which seems to delete the bills array which I passed in the "/" route. I put

<link rel="shortcut icon" href="#">

作为临时解决方案。一旦我更改了显示404页面的路线,此问题便得到了永久解决。

in the head of index.ejs as a temporary fix. Once I changed this route to render a 404 page, the issue was permanently fixed.

这篇关于即使正确渲染,EJS变量也会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆