我有一个场景,我正在uoloading一个csv文件在一个数据表和按钮点击我发送所有的行到一个端点API,他们将被处理一个接一个使用异步函数。我发送下面的负载到一个休息api,在异步函数
var b = [
{
"LastName": "LastName02",
"Email": "FN02.LN02@gmail.com",
"MobilePhone": "9198842"
},
{
"LastName": "LastName03",
"Email": "FN03.LN03@gmail.com",
"MobilePhone": "+91 9884299071"
},
]
一旦异步函数运行,我得到如下响应
对于第一条记录,我得到哪一部分在下面
resp.detailErrors = [
{
"errorCode": 7002,
"field": "communicationNumbers.number",
"value": "98842",
"message": "For country India, Mobile length must be exactly 10 numeric digits, cannot start with zero."
},
{
"errorCode": 7002,
"field": "primaryEmailId",
"value": "FN02.LN02@gmail.com",
"message": "Invalid Primary Email Address"
},
{
"errorCode": 7002,
"field": "primaryEmailId",
"value": "FN02.LN02@gmail.com",
"message": "Invalid Primary Email Address"
}
]
而对于第二张唱片,我得到
[
{
"errorCode": 7002,
"field": "primaryEmailId",
"value": "FN03.LN03@gmail.com",
"message": "Invalid Primary Email Address"
},
{
"errorCode": 7002,
"field": "primaryEmailId",
"value": "FN03.LN03@gmail.com",
"message": "Invalid Primary Email Address"
}
]
由于数据表的keyField是电子邮件,所以我想显示错误行。要显示错误行,我需要以特定的方式存储错误详细信息,如下所示
let errors = { rows: {}}
errors.rows['FN02.LN02@gmail.com'] = {
title: 'error',
messages: ['invalid email', 'invalid phone'],
fieldNames: ['Email', 'MobilePhone']
};
errors.rows['FN03.LN03@gmail.com'] = {
title: 'error',
messages: ['invalid email'],
fieldNames: ['Email']
};
我正在尝试的代码如下,但它不工作
let map = [];
let errors = { rows: {} }
var key = '';
var resp = {"detailedErrors":[{"errorCode":7002,"field":"contactEntity.communicationNumbers.number","value":"98842","message":"For country India, Mobile length must be exactly 10 numeric digits, cannot start with zero."},{"errorCode":7002,"field":"primaryEmailId","value":"FN02.LN02@gmail.com","message":"Invalid Primary Email Address"},{"errorCode":7002,"field":"primaryEmailId","value":"FN02.LN02@gmail.com","message":"Invalid Primary Email Address"}],"correlationIdentifier":"df3df8e876bce7357fcc84ccd36a76f1"}
resp.detailedErrors.forEach(res=>{
key = res.Email
if(res.value.includes('@')) {
map.push({
key:res.value,
val:res.message
});
} else {
console.log(res)
if(key == undefined) {
map.push({
key:key,
val:res.message
});
}
}
})
console.log(map)
2条答案
按热度按时间2ledvvac1#
key = res.Email
将始终将key
设置为undefined
,因为Email
在示例中的res
对象上不存在。也许您想使用res.value
?你可能也不需要Map中的空对象,而不是
let map = [{}];
可以做let map = [];
bgtovc5b2#
要以逐行方式生成错误详细信息,必须迭代原始数据的每一行,然后检查是否存在与该行关联的任何错误。***KeyField***可用于标识原始数据中的每一行,然后将其与***resp.detailErrors***数组中的错误进行匹配。
下面是应生成所需结果的代码示例:
输出: