英文:
How can I get all genres from tmdb api using redux tool kit
问题
const initialState = {
Most_Popular: [], // 显示在主页上的热门电影列表
top_rated: [], // 显示在主页上的评分最高电影列表
Movies: [], // 所有电影列表
Movie: {}, // 当前电影
isLoading: true, // 检查页面是否正在加载
isError: false,
};
export const getMoviesGenres = createAsyncThunk(
"getMoviesGenres",
async (_, thunkAPI) => {
try {
const api = `api_key=${process.env.REACT_APP_KEY_API}`;
const data = await getJSON.get(`genre/movie/list?${api}`); // 获取所有电影类型的ID
for (let i = 0; i <= data.data.genres.length; i++) {
if (!data.data.genres[i]) return; // 守卫条件
const { id, name } = data.data.genres[i];
const dataMovieGenres = [
await getJSON.get(`discover/movie?${api}&with_genres=${id}`)
];
return dataMovieGenres;
}
} catch (error) {
return thunkAPI.rejectWithValue(error);
}
}
);
// 获取电影类型
builder
.addCase(getMoviesGenres.pending, (state) => {
state.isLoading = true;
state.isError = false;
})
.addCase(getMoviesGenres.fulfilled, (state, { payload }) => {
console.log(payload);
// state.Movies = payload.data.results;
state.isLoading = false;
state.isError = false;
})
.addCase(getMoviesGenres.rejected, (state, { payload }) => {
state.isLoading = false;
state.isError = false;
toast.error(payload.message);
});
你好,我正在尝试使用Redux Toolkit从TMDB API中获取所有电影的所有类型。我创建了一个循环,直到没有更多电影可获取为止。然后,我想将所有电影放入Movies数组中。
我遇到了一个问题,不知何故我只能获取循环中的一个类型。
再次强调,我的目标是使dataMovieGenres包含所有可用的电影,然后我可以在extraReducers中处理我的数据,然后当然迭代它们并显示所有类型的电影。
正如我提到的,我只获得一个类型。
我需要一种方法来从axios获取任何数据并将其传输到数组,然后执行return。问题在于我不想使用push,因为会引发副作用。
英文:
const initialState = {
Most_Popular: [], // List Movies Popular Shown on home page
top_rated: [], // List top_rated Shown on home page
Movies: [], // All movies list
Movie: {}, // current Movie
isLoading: true, // cheak if page is loading
isError: false,
};
export const getMoviesGenres = createAsyncThunk(
"getMoviesGenres",
async (_, thunkAPI) => {
try {
const api = `api_key=${process.env.REACT_APP_KEY_API}`;
const data = await getJSON.get(`genre/movie/list?${api}`); // get all id genres
for (let i = 0; i <= data.data.genres.length; i++) {
if (!data.data.genres[i]) return; // guard clauses
const { id, name } = data.data.genres[i];
const dataMovieGenres = [
await getJSON.get(`discover/movie?${api}&with_genres=${id}`)
];
return dataMovieGenres;
}
} catch (error) {
return thunkAPI.rejectWithValue(error);
}
}
);
// get Movies Genres
builder
.addCase(getMoviesGenres.pending, (state) => {
state.isLoading = true;
state.isError = false;
})
.addCase(getMoviesGenres.fulfilled, (state, { payload }) => {
console.log(payload);
// state.Movies = payload.data.results;
state.isLoading = false;
state.isError = false;
})
.addCase(getMoviesGenres.rejected, (state, { payload }) => {
state.isLoading = false;
state.isError = false;
toast.error(payload.message);
});
Hi,
I am trying to get all movies from all genres from tmdb api using redux tool kit.
I made a for loop that will make an api call until there are no more movies.
After that I want to put all the movies into the Movies array.
I'm having a problem that I can't for some reason I only get genre 1 from the loop.
Again my goal is that dataMovieGenres
Bring me all the movies you have and then I can handle my data in extraReducers
Then of course iterate on them and display all the movies from all the categories I have.
like I mentioned I only get one genre.
I need some way to get any data from axios and transfer it to an array and then perform a return.
The problem is that I don't want to use push because of a side effect.
答案1
得分: 1
Promises can only be fulfilled once. You are seeing the getMoviesGenres
resolve after the first "inner" / nested-loop request resolves, the rest are ignored since the action was already fulfilled.
You'll want to create an array of Promises for all genres' movies that can be awaited en masse.
Example:
// 获取电影类型
builder
.addCase(getMoviesGenres.pending, (state) => {
state.isLoading = true;
state.isError = false;
})
.addCase(getMoviesGenres.fulfilled, (state, { payload }) => {
console.log(payload);
state.movies = payload; // 将数据保存到状态中
state.isLoading = false;
state.isError = false;
})
.addCase(getMoviesGenres.rejected, (state, { payload }) => {
state.isLoading = false;
state.isError = true;
});
请注意,上面的代码已被翻译成中文。
英文:
Promises can only be fulfilled once. You are seeing the getMoviesGenres
resolve after the first "inner"/nested-loop request resolves, the rest are ignored since the action was already fulfilled.
You'll want to create an array of Promises for all genres' movies that can be awaited en masse.
Example:
export const getMoviesGenres = createAsyncThunk(
"getMoviesGenres",
async (_, thunkAPI) => {
try {
const api = `api_key=${process.env.REACT_APP_KEY_API}`;
const { data } = await getJSON.get(`genre/movie/list?${api}`);
const moviesReqs = data.genres.map(async ({ id }) => {
const { data } = getJSON.get(`discover/movie?${api}&with_genres=${id}`);
return data.results; // array of movies in genre
});
const movies = await Promise.all(moviesReqs)
.then(allMovies => allMovies.flat()); // flatten array of array of movies
return movies;
} catch (error) {
toast.error(error.message); // <-- toast from here, reducer is pure
return thunkAPI.rejectWithValue(error);
}
}
);
// get Movies Genres
builder
.addCase(getMoviesGenres.pending, (state) => {
state.isLoading = true;
state.isError = false;
})
.addCase(getMoviesGenres.fulfilled, (state, { payload }) => {
console.log(payload);
state.movies = payload; // <-- save payload into state
state.isLoading = false;
state.isError = false;
})
.addCase(getMoviesGenres.rejected, (state, { payload }) => {
state.isLoading = false;
state.isError = true;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论