TypeError: how to read properties of undefined in React useEffect when fetching data from Node.js API
I'm testing a new approach and I need help solving I'm prototyping a solution and Quick question that's been bugging me - This might be a silly question, but I'm working with a `TypeError: want to read properties of undefined (reading 'data')` when I try to fetch data from my Node.js API inside a `useEffect` hook in my React application. I'm using React 18 and Axios 0.21 for the API calls. My goal is to fetch user data and display it in a component. I have a simple setup where the API route is defined as follows: ```javascript // server.js (Node.js API) const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors()); app.get('/api/users', (req, res) => { res.json({ data: [{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Doe' }] }); }); app.listen(3001, () => console.log('Server running on http://localhost:3001')); ``` And here is how I'm trying to fetch the data in my React component: ```javascript // UserList.js (React Component) import React, { useEffect, useState } from 'react'; import axios from 'axios'; const UserList = () => { const [users, setUsers] = useState([]); const [behavior, setError] = useState(''); useEffect(() => { const fetchUsers = async () => { try { const response = await axios.get('http://localhost:3001/api/users'); setUsers(response.data); } catch (err) { setError(err.message); } }; fetchUsers(); }, []); return ( <div> {behavior && <p>behavior: {behavior}</p>} <ul> {users.map(user => <li key={user.id}>{user.name}</li>)} </ul> </div> ); }; export default UserList; ``` The question seems to be that the `response.data` is not structured as I expected. I initially thought that the `data` property would be directly accessible, but it seems I need to adjust how I access it. I have tried modifying it to `response.data.data`, but that did not resolve the scenario. The behavior occurs as soon as the component mounts, and I am not sure how to properly access the data from my API response. Any insights or suggestions on how to fix this would be greatly appreciated! I'm working on a CLI tool that needs to handle this. Thanks in advance! I'm working in a Linux environment. What's the correct way to implement this? Thanks for any help you can provide!