작성
·
752
2
import React from "react";
import "./index.css";
import axios from "axios";
function MainPage() {
const [products, setProducts] = React.useState([]);
React.useEffect(function () {
axios
.get(
"https://d1a1b399-04d4-44bf-a91a-12823e4dfcec.mock.pstmn.io/products"
)
.then(function (result) {
const products = result.data.products;
setProducts(products);
})
.catch(function (error) {
console.error("에러 발생 : ", error);
});
}, []);
return (
<div>
<div id="header">
<div id="header-area">
<img src="images/icons/logo.png" />
</div>
</div>
<div id="body">
<div id="banner">
<img src="images/banners/banner1.png" />
</div>
<h1>판매되는 상품들</h1>
<div id="product-list">
{products.map(function (product, index) {
return (
<div className="product-card">
<div>
<img className="product-img" src={product.imageUrl} />
</div>
<div className="product-contents">
<span className="product-name">{product.name}</span>
<span className="product-price">{product.price}원</span>
<div className="product-seller">
<img
className="product-avatar"
src="images/icons/avatar.png"
/>
<span>{product.seller}</span>
</div>
</div>
</div>
);
})}
</div>
</div>
<div id="footer"></div>
</div>
);
}
export default MainPage;
이렇게 코드를 작성하였을 때, localhost:3000 페이지에
접속하면 위 제목 같은 오류가 나옵니다ㅠ
오타가 없는 지 몇 번이고 확인했지만 오타는 없었어요ㅠ
답변 7
3
axios의 then 안에 있는 구문을 바꿔주시면 될 것 같아요!
.then(function (result) {
const products = result.data.product;
setProducts(products);
})
3
지금 보시면 data 객체에 products를 key 하는 array가 들어있는데, 작성자님께서 짠 코드는 product로 접근하는 코드를 작성하신 것 같습니다!
포스트맨에 example쪽을 수정해주거나 코드를 data.products로 바꾸면 될 것 같아요
2
2
2
products.map(function (product, index)를 products.map(function (product, index) => 이렇게 바꿔보시면 될것 같네요?
1
해당 코드로 수정하고 구글 콘솔창을 열어본 결과
data : {
"product" : [
{
"name": "농구공",
"price": 100000,
"seller": "조던",
"imageUrl": "images/products/basketball1.jpeg",
},
{
"name": "축구공",
"price": 50000,
"seller": "메시",
"imageUrl": "images/products/soccerball1.jpg",
},
{
"name": "키보드",
"price": 10000,
"seller": "그랩",
"imageUrl": "images/products/keyboard1.jpg",
}
]
}
index.js:17 products : undefined
index.js:39 Uncaught TypeError: Cannot read property 'map' of undefined
at MainPage (index.js:39)
at renderWithHooks (react-dom.development.js:14985)
at updateFunctionComponent (react-dom.development.js:17356)
at beginWork (react-dom.development.js:19063)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at beginWork$1 (react-dom.development.js:23964)
at performUnitOfWork (react-dom.development.js:22776)
at workLoopSync (react-dom.development.js:22707)
at renderRootSync (react-dom.development.js:22670)
at performSyncWorkOnRoot (react-dom.development.js:22293)
at react-dom.development.js:11327
at unstable_runWithPriority (scheduler.development.js:646)
at runWithPriority$1 (react-dom.development.js:11276)
at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
at flushSyncCallbackQueue (react-dom.development.js:11309)
at scheduleUpdateOnFiber (react-dom.development.js:21893)
at dispatchAction (react-dom.development.js:16139)
at index.js:19
index.js:1 The above error occurred in the <MainPage> component:
at MainPage (http://localhost:3000/static/js/main.chunk.js:627:80)
at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
console.<computed> @ index.js:1
index.js:1 에러 발생 : TypeError: Cannot read property 'map' of undefined
at MainPage (index.js:39)
at renderWithHooks (react-dom.development.js:14985)
at updateFunctionComponent (react-dom.development.js:17356)
at beginWork (react-dom.development.js:19063)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at beginWork$1 (react-dom.development.js:23964)
at performUnitOfWork (react-dom.development.js:22776)
at workLoopSync (react-dom.development.js:22707)
at renderRootSync (react-dom.development.js:22670)
at performSyncWorkOnRoot (react-dom.development.js:22293)
at react-dom.development.js:11327
at unstable_runWithPriority (scheduler.development.js:646)
at runWithPriority$1 (react-dom.development.js:11276)
at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
at flushSyncCallbackQueue (react-dom.development.js:11309)
at scheduleUpdateOnFiber (react-dom.development.js:21893)
at dispatchAction (react-dom.development.js:16139)
at index.js:19
이렇게 찍힙니다.
1
안녕하세요! 현재 products state에 값이 제대로 안들어가는 것으로 보입니다.
네트워크 통신이 제대로 안되는 것으로 추측이 가네요.
axios.then 부분에 products를 가져오는 부분에 Log를 찍어서 값이 제대로 들어오는지 확인해주실 수 있을까요?
크롬 개발자도구에서 로그를 확인해보시면 답이 나올 것 같아요.
console.log('data : ',result.data)
const products = result.data.products;
console.log('products : ',products)
setProducts(products);