I'm doing a little project using the movie DB API is ReactJs, and I have problems to use async functions, i'm making the api calls in many parts of projects an worked in all of them, but now it's not working.
My axios config:
export default axios.create({
baseURL: 'https://api.themoviedb.org/3/',
params: {
api_key: process.env.REACT_APP_API,
},
});
In my container I have this
const { setMenuSelected } = useContext(MovieContext);
const [movie, setMovie] = useState({})
const [recommendations, setRecommendations] = useState([]);
const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
animateScroll.scrollToTop({ smooth: true });
setMenuSelected('');
getMovieRecommendations(setRecommendations, match.params.id, setIsLoading);
setMovie(getMovieDetails(match.params.id, setMovie));
}, [match.params.id, recommendations, isLoading]);
I'm fetching the data in other file just to organize the project
export const getMovieRecommendations = async (
setRecommendations,
movieId,
setIsLoading) => {
const res = await api.get(`/movie/${movieId}/recommendations`);
setRecommendations(res.results);
setIsLoading(false);
}
And to render the movie list I'm passing the recommendations to my component that only use map to render each movie,
<MovieList header={'Recommentadion'} movieList={recommendations} />
But the app allways crash saying "Cannot read property 'map' of undefined", i looked in the network properties in the chrome and my request is finishing with no erros.
Update:
return of console.log(JSON.stringify(res)) I removed the results itens so it wouldn't get too big
{
"data":{
"page":1,
"results":[
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
"total_pages":2,
"total_results":40
},
"status":200,
"statusText":"",
"headers":{
"cache-control":"public, max-age=14400",
"content-type":"application/json;charset=utf-8",
"etag":"W/\"56ca661f28e43dc3dd2ce41816b517c5\""
},
"config":{
"transformRequest":{
},
"transformResponse":{
},
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1,
"headers":{
"Accept":"application/json, text/plain, */*"
},
"method":"get",
"baseURL":"https://api.themoviedb.org/3/",
"params":{
"api_key":"fe5b485ed12153ca357c3275cfad6c5c"
},
"url":"https://api.themoviedb.org/3/movie/399121/recommendations"
},
"request":{
}
}
await api.get? I guess you meanawait axios.get(..)