在JSON-JavaScript中未定义值的try/catch失败
您好,我有一个脚本js,它连接一个api并接收一个json作为返回,然后插入到数据库中,但是当这个json有一个未定义的在JSON-JavaScript中未定义值的try/catch失败,javascript,node.js,api,Javascript,Node.js,Api,您好,我有一个脚本js,它连接一个api并接收一个json作为返回,然后插入到数据库中,但是当这个json有一个未定义的值时,它失败了,它停止被循环(对于卡中的x),即使它在try/catch中。有什么建议吗 注意:包含内容[data][squads][0][“cards”][x][assignees'][0][fullname']的值未定义并插入,但在此之后脚本将被中断 content=const content=wait rawResponse.json() data=var data=Ob
值时,它失败了,它停止被循环(对于卡中的x)
,即使它在try/catch
中。有什么建议吗
注意:包含内容[data][squads][0][“cards”][x][assignees'][0][fullname']
的值未定义并插入,但在此之后脚本将被中断
content=const content=wait rawResponse.json()代码>
data=var data=Object.keys(内容)代码>
squads=var squads=Object.keys(内容[数据])代码>
cards=var cards=Object.key(内容[数据][squads][0][“卡片])代码>
总指数=184
未能访问=77
印制价值观卡:
[
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25', '26', '27', '28', '29',
'30', '31', '32', '33', '34', '35', '36', '37', '38', '39',
'40', '41', '42', '43', '44', '45', '46', '47', '48', '49',
'50', '51', '52', '53', '54', '55', '56', '57', '58', '59',
'60', '61', '62', '63', '64', '65', '66', '67', '68', '69',
'70', '71', '72', '73', '74', '75', '76', '77', '78', '79',
'80', '81', '82', '83', '84', '85', '86', '87', '88', '89',
'90', '91', '92', '93', '94', '95', '96', '97', '98', '99',
... 85 more items
]
脚本:
for (x in cards) {
const parameters = [
content[data][squads][0]["cards"][x]['identifier'],
content[data][squads][0]["cards"][x]['title'],
content[data][squads][0]["cards"][x]['description'],
content[data][squads][0]["cards"][x]['status'],
content[data][squads][0]["cards"][x]['priority'],
content[data][squads][0]["cards"][x]['assignees'][0]['fullname'],
content[data][squads][0]["cards"][x]['assignees'][0]['email'],
content[data][squads][0]["cards"][x]['primaryLabels'],
content[data][squads][0]["cards"][x]['secondaryLabel'],
content[data][squads][0]["cards"][x]['swimlane'],
content[data][squads][0]["cards"][x]['workstate'],
content[data][squads][0]["cards"][x]['createdAt']
];
res = await client.query(query, parameters);
client.query(res, () => {
try {
console.log("Insert card: " + content[data]["squads"][0]["cards"][x]['title']);
} catch (error) {
console.error("Error: " + error + " - " + content[data]["squads"][0]["cards"][x]['title']);
}
})
}
输出:
Index JSON: 76
Insert card: [WIN] Update version Windows
Index JSON: 77
Error - TypeError: Cannot read property 'fullname' of undefined - [LZ] Clean /tmp/
Insert card: [LZ] Clean /tmp/
将try/catch
环绕for
循环的整个主体
用于(卡中的x){
试一试{
常数参数=[
内容[数据][小队][0][“卡片”][x][“标识符”],
内容[数据][小队][0][“卡片”][x][“标题”],
内容[数据][小队][0][“卡片”][x][“说明”],
内容[数据][小队][0][“卡片”][x][“状态”],
内容[数据][小队][0][“卡片”][x][“优先级”],
内容[数据][squads][0][“卡片”][x][“受让人”][0][“全名”],
内容[数据][squads][0][“卡片”][x][“受让人”][0][“电子邮件”],
内容[数据][小队][0][“卡片”][x][“原始标签”],
内容[数据][小队][0][“卡片”][x][“第二标签”],
内容[数据][小队][0][“卡片”][x][“泳道”],
内容[数据][小队][0][“卡片”][x][“工作状态”],
内容[数据][小队][0][“卡片”][x]['createdAt']
];
res=wait client.query(查询,参数);
...
}捕获(例外e){
错误(`cannotinsertcard${x}`);
}
}
为什么要尝试记录第一次导致错误的相同值?不应该再次调用client.query()
。client.query()
的第一个参数必须是SQL,而不是结果对象。因为它必须插入,即使它包含全名字段,其他字段也有值请注意,对于大多数说英语的人来说,所有的大写和粗体字都是大喊大叫,在线观众。try/catch
内的代码中没有关于fullname
的内容,感谢您的回复,但我仍然得到以下输出:Error-TypeError:Cannot read属性'fullname'of undefined-[LZ]Clean/tmp/如何确保当值未定义时,它只是忽略它并继续通过184个值?你不需要将它插入数据库,你只需要忽略那些未定义的值,放弃它们OK,将解决方案放入。太棒了,我的朋友,谢谢你的帮助,我希望有一天它会达到你的知识水平,祝贺你!!!