reactjs - redux 中间件的问题 - 错误:操作必须是普通对象。使用自定义中间件进行异步操作
问题描述
我有一个小问题,我无法弄清楚。我正在制作一个使用 Redux 的应用程序。而且我想创建一个动作创建者来调度另一个动作。我已经包含了 redux-thunk 中间件,但我仍然遇到同样的错误。
我试图直接在这些分派中传递对象,而不是返回这些对象的方法,但仍然发生相同的错误。此外,我一直在尝试以不同的方式应用 redux-thunk,但仍然出现相同的错误。
这是这个大动作创作者:
import {fetchMoviesBegin, fetchMoviesSuccess, fetchMoviesFailure} from
'../movie-results/movie-results.action';
const API_KEY = "d14e23d0";
export const getResults = async (inputValue) => {
return dispatch => {
dispatch(fetchMoviesBegin());
return (async () => {
try {
const getAPIS = await fetch(`http://www.omdbapi.com/?a
pikey=${API_KEY}&s=${inputValue}`);
const apiTOJSON = await getAPIS.json();
//Array ID's assignment
const returnedValues = apiTOJSON.Search;
const getIDS = returnedValues.map(item => item.imdbID);
//Array full of fetched items info
const fetchInfo = await Promise.all(getIDS.map(id =>
fetch(`http://www.omdbapi.com/?apikey=${API_KEY}&i=${id}`).then(item
=> item.json())));
//Here is the sorted array full of movies with details
fetchInfo.sort((a, b) => (a.imdbRating > b.imdbRating) ? -1 : 1);
dispatch(fetchMoviesSuccess(fetchInfo));
return fetchInfo;
}catch(error) {
return () => dispatch(fetchMoviesFailure(error));
}
})()
}
}
以下是这些操作:
import {MovieResultsTypes} from './movie-results.types';
export const setMovieResults = results => ({
type: MovieResultsTypes.FETCH_MOVIE_TITLES,
payload: results
})
export const fetchMoviesBegin = () => ({
type: MovieResultsTypes.FETCH_MOVIES_BEGIN
})
export const fetchMoviesSuccess = movies => ({
type: MovieResultsTypes.FETCH_MOVIES_SUCCES,
payload: {movies}
})
export const fetchMoviesFailure = error => ({
type: MovieResultsTypes.FETCH_MOVIES_FAILURE,
payload: {error}
})
这是带有中间件的商店:
import {createStore, applyMiddleware} from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import rootReducer from './root-reducer';
const middlewares = [logger, thunk];
export const store = createStore(rootReducer,
applyMiddleware(...middlewares));
如果有人遇到此问题,请提供帮助:/
解决方案
你能试试这个语法吗?
还有为什么你需要返回 fetchInfo?当 store 发生变化时,连接到 store 的组件会自动获取新的数据。
export function getResults(inputValue) {
return async (dispatch) => {
dispatch(fetchMoviesBegin());
try {
dispatch(fetchMoviesBegin());
const getAPIS = await fetch(`http://www.omdbapi.com/?apikey=${API_KEY}&s=${inputValue}`);
const apiTOJSON = await getAPIS.json();
//Array ID's assignment
const returnedValues = apiTOJSON.Search;
const getIDS = returnedValues.map(item => item.imdbID);
//Array full of fetched items info
const fetchInfo = await Promise.all(getIDS.map(id => fetch(`http://www.omdbapi.com/?apikey=${API_KEY}&i=${id}`)
.then(item => item.json())));
//Here is the sorted array full of movies with details
fetchInfo.sort((a, b) => (a.imdbRating > b.imdbRating) ? -1 : 1);
dispatch(fetchMoviesSuccess(fetchInfo));
// return fetchInfo;
}
catch (error) {
console.log('error: ', error);
dispatch(fetchMoviesFailure(error));
}
}
}
推荐阅读
- php - 如何在laravel中将表的数据从一个视图发送到另一个视图
- typescript - 开玩笑的打字稿请求进度管道未刷新
- react-native - 安装应用程序时 React Native 和初始数据存储
- java - 如何将值作为参数从一种方法传递到另一种方法
- c++ - 你可以在c ++中将变量用于参数吗?
- flutter - Flutter Navigator.pushNamed() 上下文
- python - 连续减去 (n,n) 数组的列表,同时保留先前减法值的列表
- bash - 如何在 python2 中使用 pkill -STOP -f 进程?
- codeceptjs - “run-multiple”命令似乎忽略了“emulate”和所有其他选项
- python - 在项目虚拟环境中启动 VScode