NodeJs - esc is not a function

霸气de小男生 提交于 2019-12-04 18:52:35

问题


I got a weird issues when trying to render an .ejs file at this specific lines

TypeError: /home/me/nodeapp/app/views/default/page/connection.ejs:66
        64|         <div class="col s12 l8">
        65|             <ul class="collapsible popout" data-collapsible="accordion"">
     >> 66|                 <%- include ../api_html/connection/connection_list.ejs %>
        67|             </ul>
        68|         </div>

Here is the error

esc is not a function

I really don't understand from where this error is coming from. I'm not using an esc function the ../api_html/connection/connection_list.ejs file which is included.

In addition to this, I'm not having this issue my local server (under Windows), this error is fired under a CentOs server where I run the exactly same nodeapp.

Any ideas would be very appreciated !

Thanks!


回答1:


First thanks for your answers.

I figured out the issue.

The error was in the included EJS file<%- include ('../api_html/connection/connection_list.ejs') %>.

For example, if you include <% include ../partials/header %> that contains <%= user.email %>, it will work perfectly when the user is defined (user is logged in) but will throw esc in not a function when it's not.




回答2:


As @Dash answered the question, it happens when you have an undefined variable in the .ejs template. There is a tricky way to find this variable and the way to do that is to put the template code directly in your ejs file and now when you run your app you can see which variable in undefined. For example, let's assume that you have index.ejs and in this file you included a template <%include partials/_pagination.ejs %>. Now if you encounter this error, simply copy and past the code within the _pagination file in your index and run your app to find which variable is undefined.




回答3:


I don't have reputation to comment so .... I just saw the same symptom. In my case I accidentally changed "currentUser.name" to "currentUser.Name" (uppercase "N") from a sample in the book "Express In Action", section 8.

in routes.js

var passport = require( "passport" );
router.use( function( req, res, next ) {
    res.locals.currentUser = req.user;
}

views/_header.ejs

<a href="/edit">
    Hello <%= currentUser.Name() %>
</a>

when I changed back to "currentUser.name" the TypeError disappeared



来源:https://stackoverflow.com/questions/42699580/nodejs-esc-is-not-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!