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

问题描述 投票:0回答:1

我正在尝试生成一个通用的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 Tables',organization_id:1,database_type_id:1,database_alias:'MAKEIT Temp Tables',database_desc:'保存MAKEIT临时表',ip_number:'localhost',port_number:'1521', service_name:'XE',用户名:'uuu',密码:'ppp'}]

如果你能提供帮助我很感激(我使用NodeJS并且更喜欢在HTML文件中生成HTML短语而不是在脚本中计算)。

javascript html arrays node.js ejs
1个回答
1
投票

records数组中的项不是数组本身(RowDataPacket)。它们是对象,因此您不能像这样使用这种for loop迭代它们。您可以使用迭代对象键

for(let prop in obj){
    //access values with obj[prop]
}
© www.soinside.com 2019 - 2024. All rights reserved.