ejs-未定义模板变量

时间:2019-01-04 21:23:00

标签: html ejs

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

我正在尝试将名为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)

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

知道了。

我不知道应该以相同的方式声明此变量。 这是我的方法:

<% var catURL; %>

并将其放在html的顶部