Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR TypeError: Cannot read property 'replace' of null

I am getting a TypeError, although my application is running properly.

"ERROR TypeError: Cannot read property 'replace' of null"

Don't get how to resolve this. Could any one give me some idea or help me to understand why this is happening? 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,
            );
        }
    }
}
like image 637
Hoss Avatar asked Mar 13 '26 02:03

Hoss


2 Answers

To fix error you need to check if variable exists before trying to replace the string. Wrap it with an if statement like this:

if(row[5]) {
    row[5] = row[5].replace(/\//g, '\/\u200B');
    const service = row[5];
}
like image 179
Rokas Rudgalvis Avatar answered Mar 15 '26 16:03

Rokas Rudgalvis


Looks like the row[5] is null. You should first check if the row[5] exists. Something like

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

You should also consider adding some handling event when the value is empty

like image 29
viks Avatar answered Mar 15 '26 15:03

viks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!