0

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/
20
  • 1
    Why are you trying to log the same value that caused the error in the first place? Commented Mar 18, 2020 at 21:05
  • You shouldn't call client.query() a second time. The first argument to client.query() must be SQL, not a result object. Commented Mar 18, 2020 at 21:07
  • Because it must be inserted even if it contains the FULLNAME field, the other fields have values Commented Mar 18, 2020 at 21:07
  • Please note that all-caps and bold read as yelling to much of the English-speaking, online audience. Commented Mar 18, 2020 at 21:07
  • There's nothing about fullname in the code inside try/catch Commented Mar 18, 2020 at 21:08

1 Answer 1

1

Wrap the try/catch around the entire body of the for loop.

for (x in cards) {
  try {
    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);
    ...
  } catch (Exception e) {
    console.error(`Couldn't insert card ${x}`);
  }
}

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply, but I still got this output: Error - TypeError: Cannot read property 'fullname' of undefined - [LZ] Clean /tmp/
How can I make sure that when the value is undefined, it simply ignores it and continues to go through the 184 values?
You don't need to insert it in the database, you can just ignore the ones that are undefined, discard them
OK, put that solution in.
Fantastic my friend, thanks for your help, I hope that one day it will reach your level of knowledge, congratulations !!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.