Hello I have a script js that connects an api and receives a json as return, and insert in database, however when this json has an undefined value, it fails, it stops being traversed by the loop (for x in cards), even if it is inside of a try/catch. Any suggestions?
NOTE: The value that has content[data][squads][0]["cards"][x]['assignees'][0]['fullname'], as undefined and inserted, however after that the script and interrupted.
content =
const content = await rawResponse.json();data =
var data = Object.keys(content);squads =
var squads = Object.keys(content[data]);cards =
var cards = Object.keys(content[data][squads][0]["cards"]);index total = 184
failed to = 77
Print values cards:
[
'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
]
Script:
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']);
}
})
}
Output:
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/
client.query()a second time. The first argument toclient.query()must be SQL, not a result object.fullnamein the code insidetry/catch