Error: Actions must be plain objects. Use custom middleware for async actions.
Error: Actions must be plain objects. Use custom middleware for async actions.
我目前正在开发一个使用 reactjs 的应用程序,该应用程序利用 react-router、redux、thunk 和 firebase 等技术。不幸的是,当我发送异步操作时出现以下错误。
我遍历了Whosebug提出的所有类似问题,仍然找不到问题所在。
如有任何帮助,我们将不胜感激。
错误:
app.js:54628 Error: Actions must be plain objects. Use custom middleware for async actions.
at dispatch (app.js:31576)
at Object.fetchBase (app.js:32220)
at Object.next (app.js:63640)
at app.js:54507
at app.js:54622
at
package.json
...
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8",
"redux-thunk": "^2.2.0",
"redux": "^3.7.1",
...
store.js
import { createStore, applyMiddleware, compose } from 'redux';
import reduxThunk from 'redux-thunk';
import { routerMiddleware } from 'react-router-redux';
import { browserHistory } from 'react-router-dom';
// Import the root reducer
import allReducers from './reducers';
// import default data
import config from './data/config';
const defaultState = {
events: [],
config,
activeGame: null,
basePath: '',
};
const router = routerMiddleware(browserHistory);
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(allReducers, defaultState,
composeEnhancers(applyMiddleware(reduxThunk, router)));
export default store;
Actions.js
...
export const fetchBase = () => {
const organizationRef = database.ref(`users/${auth.currentUser.uid}/organization`);
return dispatch => {
organizationRef.once('value', snapshot => {
dispatch({
type: ActionTypes.FETCH_BASE,
payload: snapshot.val()
});
});
};
}; // fetchBase
...
我认为 organizationRef.once
returns firebase.Promise 这就是您出现该错误的原因。
你的 action creator 看起来很不错,但是尝试像这样 ref
到 firebase 数据库:
firebase.database().ref(`/users/${uid}/employees`)
您的动作创建器看起来不错。错误消息表明,您没有正确注册 redux-thunk 中间件。
尝试导入如下:
import thunk from 'redux-thunk';
然后用
应用它
const store = createStore(allReducers, defaultState,
composeEnhancers(applyMiddleware(thunk, router)));
感谢大家的帮助。我确实找到了问题并且正在踢自己。上面的代码工作正常,问题是我在哪里注册了商店。我正在导入一个用于测试目的的辅助存储文件。真店一更新就忘了切换
我目前正在开发一个使用 reactjs 的应用程序,该应用程序利用 react-router、redux、thunk 和 firebase 等技术。不幸的是,当我发送异步操作时出现以下错误。
我遍历了Whosebug提出的所有类似问题,仍然找不到问题所在。
如有任何帮助,我们将不胜感激。
错误:
app.js:54628 Error: Actions must be plain objects. Use custom middleware for async actions. at dispatch (app.js:31576) at Object.fetchBase (app.js:32220) at Object.next (app.js:63640) at app.js:54507 at app.js:54622 at
package.json
...
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8",
"redux-thunk": "^2.2.0",
"redux": "^3.7.1",
...
store.js
import { createStore, applyMiddleware, compose } from 'redux';
import reduxThunk from 'redux-thunk';
import { routerMiddleware } from 'react-router-redux';
import { browserHistory } from 'react-router-dom';
// Import the root reducer
import allReducers from './reducers';
// import default data
import config from './data/config';
const defaultState = {
events: [],
config,
activeGame: null,
basePath: '',
};
const router = routerMiddleware(browserHistory);
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(allReducers, defaultState,
composeEnhancers(applyMiddleware(reduxThunk, router)));
export default store;
Actions.js
...
export const fetchBase = () => {
const organizationRef = database.ref(`users/${auth.currentUser.uid}/organization`);
return dispatch => {
organizationRef.once('value', snapshot => {
dispatch({
type: ActionTypes.FETCH_BASE,
payload: snapshot.val()
});
});
};
}; // fetchBase
...
我认为 organizationRef.once
returns firebase.Promise 这就是您出现该错误的原因。
你的 action creator 看起来很不错,但是尝试像这样 ref
到 firebase 数据库:
firebase.database().ref(`/users/${uid}/employees`)
您的动作创建器看起来不错。错误消息表明,您没有正确注册 redux-thunk 中间件。
尝试导入如下:
import thunk from 'redux-thunk';
然后用
应用它const store = createStore(allReducers, defaultState,
composeEnhancers(applyMiddleware(thunk, router)));
感谢大家的帮助。我确实找到了问题并且正在踢自己。上面的代码工作正常,问题是我在哪里注册了商店。我正在导入一个用于测试目的的辅助存储文件。真店一更新就忘了切换