Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot Read Property 'data' of Undefined When Fetching API Data #55490

Closed
Ndahiroloicke opened this issue Oct 22, 2024 · 1 comment
Labels
invalid Issues and PRs that are invalid.

Comments

@Ndahiroloicke
Copy link

Version

v16.0.0

Platform

Microsoft Windows 10 Pro

Subsystem

No response

What steps will reproduce the bug?

// Example of a function that fetches data from an API
function fetchData() {
let response = undefined; // Simulating an undefined response

// This line will throw a TypeError because response is undefined
const data = response.data;

console.log(data);

}

// Call the function to reproduce the bug
fetchData();

How often does it reproduce? Is there a required condition?

fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data); // Now you can safely access data
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});

What is the expected behavior? Why is that the expected behavior?

{
"data": {
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
}

What do you see instead?

TypeError: Cannot read property 'data' of undefined

Additional information

No response

@RedYetiDev RedYetiDev closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2024
@RedYetiDev RedYetiDev added the invalid Issues and PRs that are invalid. label Oct 22, 2024
@RedYetiDev
Copy link
Member

RedYetiDev commented Oct 22, 2024

v16 is EoL. Additionally, the way this issue is formatted, it's extremely difficult to see what went wrong. afaict this is behaving correctly. You can't access a property of undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

2 participants