代码之家  ›  专栏  ›  技术社区  ›  Nimrod Shai

EJS-未定义模板变量

  •  0
  • Nimrod Shai  · 技术社区  · 6 年前

    我是个网络笨蛋,找不到解决我的问题的答案:

    我试图将一个名为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 回复  |  直到 6 年前
        1
  •  1
  •   Nimrod Shai    6 年前

    知道了。

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

    <% var catURL; %>
    

    把它放在HTML的顶部