react에서 fetch의 결과값을 log에 찍었을때 Promise가 나온 경우

코덩이·2022년 6월 14일
0

리액트

목록 보기
3/3
  • 아래와 같은 코드를 친 경우
consts clickSearch = useCallback(async () => {
	const res = await fetch(
	`http://localhost:8080/room/board/search?query=${search}`
	);
	console.log(res.json());
	
});
  • Promise 객체가 출력된다

해결방법

  • Promise 객체가 출력되는 원인은 await를 사용하지 않아서이다.
consts clickSearch = useCallback(async () => {
	const res = await fetch(
	`http://localhost:8080/room/board/search?query=${search}`
	);
	const result = await res.json();
	console.log(result);
	
});
  • 위와 같이 코드를 치면 값이 정상적으로 출력되는 것을 확인 할 수 있다.
profile
개발공부중

0개의 댓글