• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

Async Await 오류가 나서 문의드립니다.

23.08.02 01:08 작성 조회수 339

0

안녕하세요.

스크립트는 아래와 같이 작성하였으며,

콘솔에는 3-12.AsyncAwait.html:20 Uncaught (in promise) TypeError: data is not iterable

at calculateSum 이라는 오류가 나왔습니다.

<!DOCTYPE html> <html> <head> <title>Document</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> var url ="https://74f8c451-bcda-46f0-820b-f63caee12b28.mock.pstmn.io/productList"; async function getData2(){ return (await axios.get(url)).data; } async function calculateSum(){ var data = await getData2(); var total = 0; for(var item of data){ total += item.price; } console.log(total); } calculateSum(); </script> </body> </html>

어디가 잘못된걸까요...

강의랑 똑같이 작성을 한거같은데..

답변 2

·

답변을 작성해보세요.

0

류재안님의 프로필

류재안

2023.08.03

저도 동일하게 안되서 일부문을 수정하니까 돌아가더라구요. 이전 강의 때 비슷한 내용으로 강의를 해주셨었는데요. 강사님 화면에서는 그대로라도 돌아가는데 왜 저는 .products를 넣어야지만 돌아가는지 까지는 모르겠습니다.

for(var item of data) -> for (var item of data.products)

0

url 에 등록한 주소로 부터 데이터가 정상적으로 받아와져야 오류가 나지 않습니다. 아래 작성하신 url로 데이터 요청했을때 데이터가 나오는지 먼저 확인하시면 좋을것 같습니다.

url ="https://74f8c451-bcda-46f0-820b-f63caee12b28.mock.pstmn.io/productList";

cis3093님의 프로필

cis3093

질문자

2023.08.02

안녕하세요, 선생님

url을 기준으로 보았을때,

promise 문으로는 출력이 잘 되지만..

Async Await 문으로만 출력이 안됩니다 ㅠ