【发布时间】:2018-02-24 20:28:26
【问题描述】:
我目前正在开发一个使用 reactjs 的应用程序,该应用程序利用了 react-router、redux、thunk 和 firebase 等技术。不幸的是,当我调度异步操作时出现以下错误。
我查看了 stackoverflow 提出的所有类似问题,但仍然找不到问题所在。
任何帮助将不胜感激。
错误:
app.js:54628 错误:操作必须是普通对象。使用自定义中间件进行异步操作。 调度时 (app.js:31576) 在 Object.fetchBase (app.js:32220) 在 Object.next (app.js:63640) 在 app.js:54507 在 app.js:54622 在
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
...
【问题讨论】:
-
不太了解您的代码(在哪里调用了操作),但显然您的操作返回函数而不是对象。
-
当用户登录网站时,我会调用
this.props.fetchBase()以获取用户在firebase 中的特定基本目录。
标签: reactjs firebase asynchronous redux redux-thunk