mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.71k stars 846 forks source link

Unable to use .keys() and .entries() in ejs. Getting error "entries is not a function" #614

Closed ganeshkbhat closed 3 years ago

ganeshkbhat commented 3 years ago

Is that possible? Using ejs:

<% if (user) { %>
  <h2><%= user.name %></h2>
<% } %>

I want to do something like this:

<% if (user) { %>
  <% users.entries().forEach(function(user){ %>
        <%= user %>
            <% }); %>
<% } %>

Getting an error:

users.entries is not a function
    at eval (eval at compile (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:662:12), <anonymous>:12:14)
    at index (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:692:17)
    at tryHandleCache (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:272:36)
    at View.exports.renderFile [as engine] (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\response.js:1012:7)
    at UserController.<anonymous> (C:\Users\ganes\Documents\gb\projects\apis\tsm\src\controller\UserController.ts:15:29)
    at step (C:\Users\ganes\Documents\gb\projects\apis\tsm\src\controller\UserController.ts:32:23)
users.keys is not a function
    at eval (eval at compile (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:662:12), <anonymous>:12:14)
    at index (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:692:17)
    at tryHandleCache (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:272:36)
    at View.exports.renderFile [as engine] (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\ganes\Documents\gb\projects\apis\tsm\node_modules\express\lib\response.js:1012:7)
    at UserController.<anonymous> (C:\Users\ganes\Documents\gb\projects\apis\tsm\src\controller\UserController.ts:15:29)
    at step (C:\Users\ganes\Documents\gb\projects\apis\tsm\src\controller\UserController.ts:32:23)

My controller:

async all(request: Request, response: Response, next: NextFunction) {
        return response.render('index', { users: {name: 25, age: 25} })
}

My index.ejs:

app.set('view engine', 'ejs');
app.set('views', './src/templates');

I am able to do this:

return response.render('index', { users: ["name", 25, "age", 25] })

<ul>
    <% users.forEach(function(user){ %>
        <%= user %>
            <% }); %>
</ul>

---- >

name 25 age 25

mde commented 3 years ago

This is almost certainly a bug in your code. First of all, you testing for the existence of user, and then trying to access users. I am closing this issue. If you can create a minimal failing test that shows a bug in the EJS library itself, please feel free to open a new one.

ganeshkbhat commented 3 years ago

Apologies for taking your time for something unrelated. I also use Python. I used Object.keys(users) and the issue was resolved. My bad.