通过索引访问数组元素的问题

时间:2019-03-06 21:27:31

标签: javascript html arrays node.js ejs

我试图生成一个通用的HTML表,其中的列名和内容根据选择而变化。 我原本希望通过id访问数组元素,但是显然它不能正常工作。

这是我的代码:

<% 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] %>不幸返回null

这是我的app.js代码块,可路由到页面:

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)
});

});

这是记录数组:

[ 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表”,     Organization_id:1     database_type_id:1,     database_alias:“ MAKEIT临时表”,     database_desc:'保留MAKEIT临时表',     ip_number:'本地主机',     port_number:'1521',     service_name:“ XE”,     用户名:'uuu',     密码:'ppp'}]

如果您能提供帮助,我非常感谢(我正在使用NodeJS,并且更喜欢在HTML文件中生成HTML短语,而不是在脚本中进行计算。)

1 个答案:

答案 0 :(得分:1)

records数组中的项目本身不是数组(RowDataPacket)。它们是对象,因此您不能像以前那样通过这种for loop遍历它们。 您可以使用

遍历对象键
for(let prop in obj){
    //access values with obj[prop]
}