**ERROR** TypeError: Cannot read property ‘replace’ of undefined [closed]

I got two object has some white space didn’t match.
when I’m using two replace function in the for..of it reports error.
if I use just one replace it works .
but object mismatch

my code is here :

 const jsonText = await tableFlyoutPanel.json.textJsonContent();
const rowContent = await alarmTable.rowContentWithHeaders(1, true);
for (const header of Object.keys(jsonText)) {
  const text = jsonText[header].replace(/s+/gm, '');
  const content = rowContent[header].replace(/s+/gm, '');
  expect(text).toStrictEqual(expect.stringContaining(content));
}

it returns error in “Content” .
ERROR CODE: TypeError: Cannot read property ‘replace’ of undefined.

   console.log(rowContent);.
 {
  Time: '2021-07-10 03:42:01.000 (1 month ago)',
  'Node name': 'sapc17',
  'Node type': '-',
  Severity: 'Major',
  'Alarm name': 'Performance Management Threshold Crossed or Reached',
  'Faulty resource': 'ManagedElement=1,SystemFunctions=1,Pm=1,PmJob=memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit=PL-3',
  Description: 'Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3'
}





       console.log(jsonText);.

{ ‘Node name’: ‘sapc17’, ‘Alarm name’: ‘Performance Management Threshold Crossed or Reached’, Severity: ‘Major’, ‘Faulty resource’: ‘ManagedElement=1,SystemFunctions=1,Pm=1,PmJob =memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit =PL-3’, ‘Fault id’: ‘628’, Description: ‘Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit ,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3’, Time: ‘2021-07-10 03:42:01.000 (1 month ago)’, ‘Event type’: ‘QUALITYOFSERVICEALARM’, ‘Node type’: ‘-‘, ‘Probable cause’: ‘-‘, Code: ‘-‘, Action: ‘-‘ }

Answer

You’re getting property keys from jsonText:

for (const header of Object.keys(jsonText)) {

And trying to use them to read properties from rowContent:

const content = rowContent[header].replace(/s+/gm, '');

But what properties do those objects have? Well, jsonText has these properties:

{
   "Node name":"sapc17",
   "Alarm name":"Performance Management Threshold Crossed or Reached",
   "Severity":"Major",
   "Faulty resource":"ManagedElement=1,SystemFunctions=1,Pm=1,PmJob =memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit =PL-3",
   "Fault id":"628",
   "Description":"Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit ,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3",
   "Time":"2021-07-10 03:42:01.000 (1 month ago)",
   "Event type":"QUALITYOFSERVICEALARM",
   "Node type":"-",
   "Probable cause":"-",
   "Code":"-",
   "Action":"-"
}

And rowContent has these properties:

{
  Time: '2021-07-10 03:42:01.000 (1 month ago)',
  'Node name': 'sapc17',
  'Node type': '-',
  Severity: 'Major',
  'Alarm name': 'Performance Management Threshold Crossed or Reached',
  'Faulty resource': 'ManagedElement=1,SystemFunctions=1,Pm=1,PmJob=memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit=PL-3',
  Description: 'Observed value: 80; Threshold level: 80; MeasurementType: ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit,MeasurementType=Mem.PercentUsed; Threshold Direction: INCREASING; MO instance: OSProcessingUnit=PL-3'
}

At a glance it’s pretty clear that jsonText contains properties which rowContent does not contain. So when you attempt to read those properties from rowContent they will be undefined. And trying to invoke .replace on undefined will fail.

It’s not really clear what this code is trying to do. But the one thing that is clear is that you can’t use a property that doesn’t exist. Perhaps you could check for the property before trying to use it? For example:

for (const header of Object.keys(jsonText)) {
  if (rowContent[header] !== undefined) {
    const text = jsonText[header].replace(/s+/gm, '');
    const content = rowContent[header].replace(/s+/gm, '');
    expect(text).toStrictEqual(expect.stringContaining(content));
  }
}

Or perhaps you didn’t intend to read from rowObject in the first place and this is a typo? Or perhaps these objects should be the same structure and there’s a mistake somewhere else in the code? It’s impossible to know given the content of the question. But the source of the error itself is simple, you can’t use an object property that doesn’t exist.