0

I am trying to generate a generic HTML table in which the column names and the content changes depending on the selection. I was expecting to access array elements by id but apparently it is not working properly.

Here is my code:

<% records.forEach(function(record){ %>
<tr role="row" class="odd">
    <% for (var i = 0; i < noOfCols; i++) { %>
        <td tabindex="0"><%= record[i] %></td>
   <% } %>                                        
        <td nowrap="">
            <span class="dropdown">
                <a href="#" class="btn m-btn m-btn--hover-brand m-btn--icon m-btn--icon-only m-btn--pill" data-toggle="dropdown" aria-expanded="true">
                  <i class="la la-ellipsis-h"></i>
                </a>
                <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="#"><i class="la la-edit"></i> Edit Details</a>
                    <a class="dropdown-item" href="#"><i class="la la-leaf"></i> Update Status</a>
                    <a class="dropdown-item" href="#"><i class="la la-print"></i> Generate Report</a>
                </div>
            </span>
                <a href="#" class="m-portlet__nav-link btn m-btn m-btn--hover-brand m-btn--icon m-btn--icon-only m-btn--pill" title="View">
                    <i class="la la-edit"></i>
                </a>
        </td>
</tr> <% }); %>

<%= record[i] %> is returning null unfortunately.

Here is my app.js code block to route to the page:

app.get('/edit/:tblId', async function(req, res, next) {
console.log("***************** Edit Table *******************");

const allDatabases  = await dbInstance.genericQuery(dbQueries.allDbSQL);
const allTables     = await dbInstance.genericQuery(dbQueries.allTbSQL);

const singletable   = await dbInstance.genericQuery(dbQueries.singleTableSQL.replace('{tblId}', req.params.tblId));
const tableName     = await dbInstance.getTableName(req.params.tblId);
const columnNames   = await dbInstance.genericQuery(dbQueries.headersTbSQL.replace('{tblId}', req.params.tblId));

const records       = await dbInstance.getTableRecords(tableName);
const noOfCols      = await dbInstance.genericQuery(dbQueries.headersTbSQL.replace('col.column_name', 'count(1) colNo').replace('{tblId}', req.params.tblId));

console.log('SQL: ' + dbQueries.headersTbSQL.replace('col.column_name', 'count(1) colNo').replace('{tblId}', req.params.tblId))

console.log('noOfCols: ' + noOfCols[0].colNo);
console.log(records);
res.render('edit_table_new', {
    parameters: arrayOpsInstance.getMatchDBWithTables(allDatabases, allTables),
    tblName: tableName,
    pagePathDatabase: 'EDIT DATA - ' + singletable[0].database_name.toUpperCase(),
    pagePathSchema: singletable[0].schema_name.toUpperCase(),
    tableDesc: singletable[0].table_desc,
    headers: columnNames,
    records: records,
    noOfCols: Number(noOfCols[0].colNo)
});

});

And here is the records array:

[ RowDataPacket {
database_id: 1,
database_name: 'REFERA',
organization_id: 1,
database_type_id: 3,
database_alias: 'Refera Repository',
database_desc: 'Holds Refera Repository Information',
ip_number: 'localhost',
port_number: '3306',
service_name: 'refera',
username: 'uuu',
password: 'ppp' },

RowDataPacket { database_id: 2, database_name: 'MAKEIT Tables', organization_id: 1, database_type_id: 1, database_alias: 'MAKEIT Temp Tables', database_desc: 'Holds MAKEIT Temp Tables', ip_number: 'localhost', port_number: '1521', service_name: 'XE', username: 'uuu', password: 'ppp' } ]

I do appreciate if you can help (I am using NodeJS and prefer generate HTML phrases within HTML file rather than calculating in script).

18
  • 7
    Please edit your question to include the framework you're using. <%= %> is used by a number of them. Commented Mar 6, 2019 at 21:28
  • Thanks Heretic Monkey, I have stated that I am using NodeJS.. Commented Mar 6, 2019 at 21:32
  • 1
    Okay but what templating engine are you using? NodeJS is the javascript runtime environment, not the library that is giving you the <%= %> syntax. Commented Mar 6, 2019 at 21:32
  • I use ejs if you are asking that.. Commented Mar 6, 2019 at 21:33
  • 1
    It's not node related Commented Mar 6, 2019 at 21:34

1 Answer 1

1

Items in records array are not array themselves (RowDataPacket). They are objects and hence you can't iterate through them with this kind of for loop as you did. You can iterate over object keys using

for(let prop in obj){
    //access values with obj[prop]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.