error 발생 : TypeError: products is undefined
이전까지는 다 잘되다가 마지막에 innerHTML을 넣으니 이런 오류가 뜨는데 뭘 잘못했는지 도저히 모르겠습니다.
<html>
<head>
<title>그랩마켓</title>
<link href="index.css" type="text/css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="header">
<div id="header-area">
<img src="images/icons/logo.png" alt="" />
</div>
</div>
<div id="body">
<div id="banner">
<img src="images/banners/banner1.png" alt="" />
</div>
<h1>판매되는 상품들</h1>
<div id="product-list"></div>
</div>
<div id="footer"></div>
</body>
<script>
axios.get("https://0d1b6acd-73da-48c3-8cca-572d9b17eca5.mock.pstmn.io/products")
.then(function (result) {
console.log("통신결과 : ", result);
const products = result.data.products;
let productsHtml = "";
for (var i = 0; i < products.length; i++) {
let product = products[i];
productsHtml =
productsHtml +
'<div class="product-card">' +
"<div>" +
'<img class="product-img" src="' +
product.imageUrl +
'" alt="" />' +
"</div>" +
'<div class="product-contents">' +
'<span class="product-name">' +
product.name +
"</span>" +
'<span class="product-price">' +
product.price +
"원" +
"</span>" +
'<div class="product-seller">' +
'<img class="product-avatar" src="images/icons/avatar.png" alt="" />' +
"<span>" +
product.seller +
"</span>" +
"</div>" +
"</div>" +
"</div>";
}
document.querySelector("#product-list").innerHTML = productsHtml;
})
.catch(function (error) {
console.error("error 발생 : ", error);
});
</script>
</html>
넵 선생님 포스트맨 코드입니다!