I have two API endpoints that I am retrieving data from and I want to display the number of records in each. This is my code so far, which sort of works, and test6tg5ing the API in Postman returns the correct data -
const [orders, setOrders] = useState([]);
useEffect(() => {
const fetchData = async () => {
const resSeven = await axios('http://localhost:5000/api/7daycheck');
const resThirty = await axios('http://localhost:5000/api/30daycheck');
setOrders({ seven: resSeven.data, thirty: resThirty.data });
};
fetchData();
}, []);
Now, When I add console.log(orders.seven); , the console shows this -
So when I add the line <p>{orders.seven.length}</p> in my JSX code it throws the error "TypeError: Cannot read property 'length' of undefined". What's going on?

const [orders, setOrders] = useState([]);) then set it to an object (setOrders({ seven: resSeven.data, thirty: resThirty.data });); it does not make a lot of sense. Anyway, the issue is due to the fact thatorders.sevenisundefineduntil you get back the response from your fetch call. If you change<p>{orders.seven.length}</p>with<p>{orders && orders.seven && orders.seven.length}</p>you should not see that error anymore.