Reputation: 33
I am new to we programming. I got stuck with rendering JavaScript variables.
Could you please give me an advice to resolve?
Inside .ejs
, I am trying to iterate over table
<tr>
<% bodies.forEach(function(body){ %>
<% try (typeof body.rowNum !== 'undefined') { %>
<th scope="row"><%= body.rowNum %></th>
<% if(typeof body.tick !== 'undefined') { %>
<td><%= body.tick %></td>
<% } %>
<% if(typeof body.exDiv !== 'undefined') { %>
<td><%= body.exDiv %></td>
<% } %>
<% if(typeof body.divYield !== 'undefined') { %>
<th><%= body.divYield %></th>
<% } %>
<% if(typeof body.qty !== 'undefined') { %>
<th><%= body.qty %></th>
<% } %>
<% if(typeof body.total !== 'undefined') { %>
<th><%= body.total %></th>
<% } %>
<% } %>
<% }) %>
</tr>
Below is .js
file:
app.post("/", function(req, res){
var bodies = {rowNum: 1, tick: tick, exDiv: exDiv, divYield: dividend, qty: 50, total: total}
res.render("list", {bodies:bodies});
});
The error message I am getting
SyntaxError: Unexpected token '(' in C:\Users\yoonl\Desktop\DividendTracker\views\list.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true
as an option.
at new Function ()
at Template.compile (C:\Users\yoonl\Desktop\DividendTracker\node_modules\ejs\lib\ejs.js:661:12)
at Object.compile (C:\Users\yoonl\Desktop\DividendTracker\node_modules\ejs\lib\ejs.js:396:16)
at handleCache (C:\Users\yoonl\Desktop\DividendTracker\node_modules\ejs\lib\ejs.js:233:18)
at tryHandleCache (C:\Users\yoonl\Desktop\DividendTracker\node_modules\ejs\lib\ejs.js:272:16)
at View.exports.renderFile [as engine] (C:\Users\yoonl\Desktop\DividendTracker\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\yoonl\Desktop\DividendTracker\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\yoonl\Desktop\DividendTracker\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\yoonl\Desktop\DividendTracker\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\yoonl\Desktop\DividendTracker\node_modules\express\lib\response.js:1012:7)
[Question] I am not familiar with async
if the following issue can be fixed by adding async function, could you please share an example how to use it?
Thank you,
Upvotes: 1
Views: 733
Reputation: 24565
The are a few things you need to fix..
1.) you are expecting an array
in your template, since you use bodies.forEach(...)
but you pass an object. Fix this by doing:
var bodies = [{rowNum: 1, tick: 123, exDiv: 123, divYield: 123, qty: 50, total: 123}];
res.render("list", {bodies}); // use Object Property Value Shorthand instead of {bodies:bodies}
2.) <% try (typeof body.rowNum !== 'undefined') { %>
This is incorrect syntax, I think you wanted to use if
instead:
<% if (typeof body.rowNum !== 'undefined') { %>
Your error has nothing to do with async
in this case - it's just incorrect syntax and incorrect data passed to the template
Upvotes: 1