React 응답오류

sam_il·2022년 8월 20일

✅ 프로젝트 진행 중 발생한 오류
(방명록 블로그 오류)

❗ TypeError: Cannot read property 'id' of undefined at Function~

  • fetch post할때 json data에 id값을 주지 않았기 때문에 발생한 오류.

POST 호출 (fetch api)

  • 데이터 생성

method 옵션을 POST로 지정, headers 옵션을 통해 JSON 포멧을 사용한다고 알려주고 요청 전문을 JSON 포맷으로 직렬화화해서 body 옵션에 설정해준다.

fetch("https://jsonplaceholder.typicode.com/posts", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    title: "Test",
    body: "I am testing!",
    userId: 1,
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data));
  
//코드 출처 : https://www.daleseo.com/js-window-fetch/

📌 참고자료
https://www.zerocho.com/category/NodeJS/post/579b4ead062e76a002648af7

https://stackoverflow.com/questions/52030845/json-server-getting-error-after-post-request

https://www.daleseo.com/js-window-fetch/

profile
🍀

0개의 댓글