M
M
Magnus Keef2018-12-13 11:39:00
JavaScript
Magnus Keef, 2018-12-13 11:39:00

How to loop through objects in ejs?

This is node js
var punishments = {};  //объект "штрафы"

app.get('/driver', function (req, res) {
  res.render('driver',
      {
          PunNum:punishments.num_punishment,
          PunName:punishments.name_breakdown,
          PunSum:punishments.coast,
          PunCount:'0'//должно быть колличество объектов(кол-во штрафов)
      });
});

function queryDatabasePunish() {
    request = new Request(`SELECT punishments.num_punishment, breakdowns.coast, punishments.name_breakdown FROM breakdowns INNER JOIN punishments ON breakdowns.num_breakdown = punishments.num_breakdown WHERE (punishments.num_ptc = '${password}')`, function (err, rowCount, rows) {
        console.log(rowCount + ' row(s) вернулось из штрафов');
    });

    request.on('row', function (columns) {
        columns.forEach(function (column) {
            punishments[column.metadata.colName] = column.value;//заполнение объекта
        });
    });

    abcdef.execSql(request);
}
. Here I make a query to the database and get info (column names and values). I put the received information into an object punishments = {};
. The database table contains the following records:
database table
5c1217f181d23473323219.png

Now I display only the last entry in the object on the page
Result(Figure)
5c1218223a05f638052784.png
Output in ejs
<div id="block_punish" class="block_punish">
                <h2>Информация о штрафах</h2>
                <i>У ВАС <%=PunCount%> ШТРАФОВ</i>
                <p>Номер протокола: <%=PunNum%></p>
                <p>Название нарушения: <%=PunName%></p>
                <P>Дата фиксации нарушения: </P>
                <p>Сумма: <%=PunSum%></p>
            </div>

Tell me how to display all records from the database in ejs
this is how it should be
5c121a85bbd2d199981874.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Magnus Keef, 2018-12-15
@SecurityYourFingers

<% for (var n=0; n<(locals.length); n++) {%>
                <% if (JSON.stringify(locals[n * 3]) === undefined) break;%>
                <p>Номер протокола:         <%- JSON.stringify(locals[n * 3])%></p>
                <p>Название нарушения:      <%- JSON.stringify(locals[3 * n + 2])%></p>
                <P>Дата фиксации нарушения:                                     </P>
                <p>Сумма:                   <%- JSON.stringify(locals[3 * n + 1])%></p>
                <hr>
                <% } %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question