ejs - template variable is not defined

Viewed 6195

i'm a web noob and I couldn't find an answer that solved my case:

I'm trying to pass on a variable called catURL to my html file but when I try to render it, it fails.

This is how I pass the variable:

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

This is where I try to show it in my 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>

This is the error msg that I get:

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)

Appreciate your help

1 Answers

Got it.

I wasn't aware that I should declare this variable the same way. This is how I did it:

<% var catURL; %>

And put it at the top of the html

Related