Studyyyyy
Promise.allSettled에서 result.value가 undefined로 나오는 오류 본문
https://stackoverflow.com/questions/53412109/fetch-returns-undefined-when-imported
Fetch returns undefined when imported
I have a function that fetches data from the url and is supposed to return it: const fetchTableData = () => { fetch('https://api.myjson.com/bins/15psn9') .then(result => result.json()) ...
stackoverflow.com
함수 안에서 fetch자체를 한번 더 return 해줘야한다.
ex)
잘못된 예시
const fetchTableData = () => {
fetch('https://api.myjson.com/bins/15psn9')
.then(result => result.json())
.then(data => {
return data;
})
}
export default fetchTableData;
올바른 예시
const fetchTableData = () => {
const fetchedData = fetch('https://api.myjson.com/bins/15psn9')
.then(result => result.json())
.then(data => {
return data;
})
return fetchedData;
}
export default fetchTableData;
슬랙에 올려논 글들이 1년이 넘어 지워지기 전에 정리중
'자바스크립트, 타입스크립트' 카테고리의 다른 글
페이지 프린트시 각 페이지마다 header, footer 넣어주기 (0) | 2024.08.29 |
---|---|
fetch abort controller (0) | 2024.08.29 |
커스텀 이벤트 (0) | 2024.08.28 |
자바스크립트 랜덤 숫자 중복 없이 추출하기 (0) | 2022.05.13 |
select 내에 선택된 option value 가져오는 법 (0) | 2022.04.27 |