안녕하세요. 지금 react 사용해서 도서 api 작업중입니다. 지금 이해가 안되는게 나와서 그런데
도와주십시오. 객체와 함수를 export 했는데 결과가 다릅니다.
솔직히 어디서 문제가 발생한지 몰라서 api관련이라 했지만
함수와 객체 문제 같습니다…
api파일에서 bestSellerApi를 객체형태로
const api = axios.create({
baseURL: "api/",
params: {
key: process.env.REACT_APP_API_KEY,
output: 'json'
}
})
export const bestSellerApi = {
bestSeller: () => api.get("bestSeller.api", {
params: {
categoryId: 100
}
})
}
한 다음
async componentDidMount() {
try {
const bestBook = await bestSellerApi.bestSeller();
console.log(bestBook)
}
이렇게 bestSellerApi.bestSeller()로 받으면 결과가 잘 나옵니다.
그런데
bestSellerApi를 함수로
export const bestSellerApi = async () => {
await api.get("bestSeller.api", {
params: {
categoryId: 100
}
})
}
한 다음
async componentDidMount() {
try {
const bestBook = await bestSellerApi();
console.log(bestBook)
}
이렇게 bestSellerApi()로 받으면 결과가 undefined가 나옵니다.
async await 넣고 빼고 다 해봤는데 undefined가 나옵니다…
함수로 하면 뭐가 문제라서 undefined가 나올까요 ? 곰곰이 생각해봐도 모르겠습니다.
ㅠ궁금해서 다른걸 못하겠습니다.