1

Error:

TypeError: Cannot read property '0' of undefined (line 59)

Here is the script where such an error appears:

Line 59        if (obj_data.data[int_i].entities.urls[0].expanded_url != undefined){
Line 60          array_Expanded_url.push([obj_data.data[int_i].entities.urls[0].expanded_url]);
Line 61        } else {
Line 62         array_Expanded_url.push([""]); 
Line 63        }

The idea of if was exactly when the value was undefined, it would define in else the empty value, but this is not happening. Are there any errors in the script visibly that I can't see?

1
  • 1
    If would be easier to help you if you provided a minimal reproducible example rather than a code fragment. Commented Jun 14, 2021 at 0:11

1 Answer 1

1

From your error message, I thought that in that situation, urls of obj_data.data[int_i].entities.urls might not be included. So in order to avoid this, how about the following modification?

Modified script:

if (
  obj_data.data[int_i].entities.hasOwnProperty("urls") &&
  Array.isArray(obj_data.data[int_i].entities.urls) &&
  obj_data.data[int_i].entities.urls[0].expanded_url != undefined
) {
  array_Expanded_url.push([obj_data.data[int_i].entities.urls[0].expanded_url]);
} else {
  array_Expanded_url.push([""]); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfectly solved the error, thank you very much!

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.