我收到以下代码的错误:
未捕获的类型错误:无法读取未定义(读取"0"(的属性
我试着使用可选的链接,但它不起作用。。。
const [deliveredOrders, setDeliveredOrders] = useState([]);
useEffect(() => {
const getDeliveredOrders = async () => {
const res = await axios.get(
`/api/orders`);
setDeliveredOrders(res.data);
};
getDeliveredOrders();
}, []);
console.log(deliveredOrders?.delivered[0]?.updatedAt);
因为可选链接了错误的属性,所以delivered
就是undefined
应该是:
deliveredOrders?.delivered?.[0]?.updatedAt