错误TypeError:无法读取null的属性“替换”

时间:2019-07-23 20:57:10

标签: angular typescript

尽管我的应用程序运行正常,但我遇到了TypeError。

  

“错误TypeError:无法读取null的属性“替换””

如何解决这个问题? enter image description here

if (isInitial) {

                  // Add Data Rows (initially)
                  for(const row of rows) {

                    const clientVisitGuid = row[2];
                    const location = row[3];

                    row[5] = row[5].replace(/\//g, '\/\u200B');
                    const service = row[5];

                    const rowId = `${service || 'zzz'}_${location}_${clientVisitGuid}`;
                    const sortString = `${service || 'zzz'}_${location}_${clientVisitGuid}`;
                    const daysPastDischarge = parseInt(row[6]);
                    const isMarked = parseInt(row[7]) === 1 ? true : false;

                    if (cbt.hasBodyRow(rowId) === false) {

                      const values: Array<[string, string | number]> = [];
                      let isAllClear = true;
                      for(let i = 0; i < row.length; i++) {
                        const colId: string = cbt.headerRow.headerCols[i].id;
                        const colVal: string | number = row[i];
                        if (cbt.headerRow.headerCols[i].isFlag === true && colVal !== 1) {
                          isAllClear = false;
                        }
                        values.push([colId, colVal]);
                      }

                      const valueHash = hash(JSON.stringify(row));

                      cbt.addBodyRow(
                        rowId,
                        values,
                        valueHash,
                        sortString,
                        daysPastDischarge,
                        isMarked,
                        isAllClear,
                        true,
                      );
                    }
                  }

2 个答案:

答案 0 :(得分:1)

看起来row[5]为空。您应该首先检查row[5]是否存在。

if(row[5]) {
     row[5] = row[5].replace(/\//g, '\/\u200B');
     const service = row[5];
} 

您还应该考虑在值为空时添加一些处理事件

答案 1 :(得分:0)

要修复错误,您需要在尝试替换字符串之前检查变量是否存在。用如下if语句包装它:

if(row[5]) {
    row[5] = row[5].replace(/\//g, '\/\u200B');
    const service = row[5];
}