Ejs doesn’t render ejs file

I want to make a queue system for to create pdf file. I created a node server and used Express framework. Also I used rabbitmq for the queue system. I set view engine ejs

app.set('view engine', 'ejs');
app.use(Express.static(__dirname + "/views"));

My folder structure is

consumers
   --consumer_report.js
views
   report.ejs
   report
       --environment.ejs
       --consultans.ejs
       --map.ejs

When a user wants to create a pdf, I redirect it to the queue. The queue is calculating some datas. After calculating I’m using render ejs file.

createReport(msg.user, msg.reportID, msg.type, msg.packetName, (err, data) => {
    data.packet = msg.packetName;

    let dirUrl = __dirname + "/../views/report.ejs";
    let opts = {
        async: true
    }

    ejs.renderFile(dirUrl, data, opts, (err, html) => {
        if (err) return console.error(err);
        console.log("html", html);
    });
});

Report Ejs

<!DOCTYPE html>
<html lang="en" style="zoom:0.75;">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <link rel="stylesheet" href="<%= host %>/css/rp.css">
    <script src="<%= host %>/js/jquery3.2.1.min.js"></script>
    <script src="<%= host %>/js/highcharts.js"></script>
    <script src="<%= host %>/js/rp.js"></script>
</head>
<body>
<% for(let i of index){brackets = i.i_page; title= i.name %>
<% if(brackets=="environment") {%> <%- include report/environment.ejs %> <% } %>
<% if(brackets=="consultant") {%> <%- include report/consultants.ejs %> <% } %>
<% if(brackets=="map") {%> <%- include report/map.ejs %> <% } %>
<% } %>
</body>
</html>

When render the ejs file I get this error

Error SyntaxError: missing ) after argument list in /home/aaa/Desktop/projects/report/consumers/../views/report.ejs while compiling ejs

I couldn’t find the error. Where is my mistake?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you need to change include syntax to include(path), as per docs:

NOTE: Include preprocessor directives (<% include user/show %>) are
not supported in v3.0+.
https://github.com/mde/ejs

try this:

<!DOCTYPE html>
<html lang="en" style="zoom:0.75;">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <link rel="stylesheet" href="<%= host %>/css/rp.css">
    <script src="<%= host %>/js/jquery3.2.1.min.js"></script>
    <script src="<%= host %>/js/highcharts.js"></script>
    <script src="<%= host %>/js/rp.js"></script>
</head>
<body>
<% for(let i of index){brackets = i.i_page; title= i.name %>
<% if(brackets=="environment") {%> <%- include('report/environment.ejs') %> <% } %>
<% if(brackets=="consultant") {%> <%- include('report/consultants.ejs') %> <% } %>
<% if(brackets=="map") {%> <%- include('report/map.ejs') %> <% } %>
<% } %>
</body>
</html>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x