gpt4 book ai didi

html - ejs - 未定义模板变量

转载 作者:行者123 更新时间:2023-12-05 07:27:09 25 4
gpt4 key购买 nike

我是网络菜鸟,我找不到解决我的问题的答案:

我试图将一个名为 catURL 的变量传递给我的 html 文件,但是当我尝试呈现它时,它失败了。

这是我传递变量的方式:

res.render('index', {catURL: url, error: null});

这是我尝试在我的 html 中显示它的地方:

<div id="Wax on" class="tabcontent">
<h3>Wax on</h3>
<p>Dont forget to wax off!!!</p>
<% if(catURL !== null){ %>
<img src="<%= catURL %>" />
<% } %>
</div>

这是我收到的错误信息:

ReferenceError: /Users/nimrodshai/Documents/Projects/WeatherJS/views/index.ejs:21
19| <h3>Wax on</h3>
20| <p>Dont forget to wax off!!!</p>
>> 21| <% if(catURL !== null){ %>
22| <img src="<%= catURL %>" />
23| <% } %>
24| </div>

catURL is not defined
at eval (eval at compile (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:11:8)
at returnedFn (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/view.js:135:8)
at tryRender (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/application.js:640:10)
at Function.render (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/response.js:1008:7)
at /Users/nimrodshai/Documents/Projects/WeatherJS/server.js:14:6
at Layer.handle [as handle_request] (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/layer.js:95:5)
at /Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:335:12)
at next (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:317:13)
at /Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:335:12)
at next (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/express/lib/router/index.js:275:10)

感谢您的帮助

最佳答案

明白了。

我不知道我应该以同样的方式声明这个变量。我是这样做的:

<% var catURL; %>

并将其放在html的顶部

关于html - ejs - 未定义模板变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com