简体   繁体   中英

Data is not Render into EJS Page in Node JS

I am trying to render the data to EJS page but I was not able to send the data into EJS page. Currently, I am receiving the data as a recordset from MSSQL database. Below screenshot gives the outcome of console.log(rows).

[ https://i.stack.imgur.com/CItXQ.jpg][1]

Coding for Database:

app.get('/data', receiveData);

function receiveData(req, res) {
    db.executeSql("SELECT * FROM arduino", function (recordsets, err, ) {
        var data = JSON.stringify(recordsets);

        if (err) {
            httpMsgs.show500(request, res, err);
        }
        else {
            var    Jdata = JSON.parse(data);
            console.log(Jdata);
            res.render('arduino',{Jdata:Jdata});
        }
    });
}

Coding for Ejs

        <table border="1" cellpadding="7" cellspacing="7">
        <tr>
            <th> - </th>
            <th>ID</th>
            <th>Machine</th>
            <th>Start Time</th>
            <th>End Time</th>
            <th>Length Time</th>
            <th> Day/Night</th>
            <th>Job Number</th>
        </tr>
        <% if(Jdata.length){

        for(var i = 0;i < Jdata.length;i++) { %>
        <tr>
            <td><%=(i+1)%></td>
            <td> </td>
            <td><%=Jdata.recordset[0].Id %></td>
            <td><%=Jdata.recordset[0].StartTime%></td>
            <td><%=Jdata.recordset[0].EndTime%></td>
            <td><%=Jdata.recordset[0].LengthTime%></td>
            <td><%=Jdata.recordset[0].Day%></td>
            <td><%=Jdata.recordset[0].JobNumber%></td>

        </tr>
        <% }

        }else{ %>
            <tr>
                <td colspan="3">No Data</td>
            </tr>
        <% } %>

    </table>

It would be great if anyone can help me out.

Thanks.

I hope below answer will fix your problem. You are using Jdata.length instead of Jdata.recordset.length. As per the console log, I understand this.

<table border="1" cellpadding="7" cellspacing="7">
    <tr>
        <th> - </th>
        <th>ID</th>
        <th>Machine</th>
        <th>Start Time</th>
        <th>End Time</th>
        <th>Length Time</th>
        <th> Day/Night</th>
        <th>Job Number</th>
    </tr>
    <% if(Jdata.recordset.length){

    for(var i = 0;i < Jdata.recordset.length;i++) { %>
    <tr>
        <td><%=(i+1)%></td>
        <td> </td>
        <td><%=Jdata.recordset[0].Id %></td>
        <td><%=Jdata.recordset[0].StartTime%></td>
        <td><%=Jdata.recordset[0].EndTime%></td>
        <td><%=Jdata.recordset[0].LengthTime%></td>
        <td><%=Jdata.recordset[0].Day%></td>
        <td><%=Jdata.recordset[0].JobNumber%></td>

    </tr>
    <% }

    }else{ %>
        <tr>
            <td colspan="3">No Data</td>
        </tr>
    <% } %>

</table>

Oh, that was easy, don't use <%, use <%- instead. For example:

<%- Jdata.length %>

<%= will render in HTML <%- will render variables (as they are, eval)

Each line of the injected scripts must be wrapped in <% ...your snippet line... %> so I would fixed your code into:

    <% if(Jdata.length) { %>
    <% for(var i = 0;i < Jdata.length;i++) { %>
    <tr>
        <td><%=(i+1)%></td>
        <td> </td>
        <td><%=Jdata.recordset[0].Id %></td>
        <td><%=Jdata.recordset[0].StartTime%></td>
        <td><%=Jdata.recordset[0].EndTime%></td>
        <td><%=Jdata.recordset[0].LengthTime%></td>
        <td><%=Jdata.recordset[0].Day%></td>
        <td><%=Jdata.recordset[0].JobNumber%></td>

    </tr>
    <% } %>

    <% }else{ %>
        <tr>
            <td colspan="3">No Data</td>
        </tr>
    <% } %>

In my case, the error was that I was trying to access an array instead of an single object.
So, I changed model.find() to

model.findOne({username:req.params.username},(err,user)=>{
    if(err) res.send(err)
     res.render("home",{user:user})
   })

on ejs you get these user proprieties : <p><%=user.username%></p> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM