javascript - **错误** TypeError:无法读取未定义的属性“替换”
问题描述
我有两个对象有一些空白不匹配。
当我在 for..of 中使用两个替换功能时,它会报告错误。
如果我只使用一个替换它就可以了。
但对象不匹配
我的代码在这里:
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));
}
它在 "Content" 中返回错误。
错误代码:TypeError:无法读取未定义的属性“替换”。
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);.
{ '节点名称': 'sapc17', '警报名称': '性能管理阈值已超过或达到', 严重性: '主要', '故障资源': 'ManagedElement=1,SystemFunctions=1,Pm=1,PmJob = memoryLoadThresholdJob,MeasurementReader=memoryLoad_mr:OSProcessingUnit =PL-3', 'Fault id': '628', 描述: '观察值: 80; 门槛等级:80;MeasurementType:ManagedElement=1,SystemFunctions=1,Pm=1,PmGroup=OSProcessingUnit,MeasurementType=Mem.PercentUsed;阈值方向:增加;MO 实例:OSProcessingUnit=PL-3',时间:'2021-07-10 03:42:01.000(1 个月前)','事件类型':'QUALITYOFSERVICEALARM','节点类型':'-','可能原因':'-',代码:'-',操作:'-' }
解决方案
您从以下位置获取属性密钥jsonText
:
for (const header of Object.keys(jsonText)) {
并尝试使用它们从以下位置读取属性rowContent
:
const content = rowContent[header].replace(/s+/gm, '');
但是这些对象有什么属性呢?嗯,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":"-"
}
并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'
}
一目了然,包含不jsonText
包含的属性很明显。rowContent
因此,当您尝试从中读取这些属性时,rowContent
它们将是undefined
. 并且尝试调用.replace
onundefined
将失败。
目前还不清楚这段代码试图做什么。但有一点很清楚,您不能使用不存在的属性。也许您可以在尝试使用之前检查该属性?例如:
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));
}
}
或者,也许您一开始并不打算阅读rowObject
,这是一个错字?或者也许这些对象应该是相同的结构并且代码中的其他地方有错误?鉴于问题的内容,不可能知道。但是错误本身的来源很简单,不能使用不存在的对象属性。
推荐阅读
- java - java中的标准标量,如numpy
- amazon-web-services - How to set up dev environment with AWS Mobile hub?
- html - Elements with both relative and absolute positioning
- python - 从 json 模式构建 spark 模式
- sas - 如何在 SAS 中将集群绘制成散点图?
- c# - C# 中的闭包,带有 lambdas 和匿名方法
- python - Performing Function per Calendar Date
- asp.net-core-mvc - 格式化输入时间跨度
- c - 在 C 中使用多个 For 循环
- javascript - http响应的属性值后未定义角度变量4