Search code examples
reactjsfirebaseasynchronousreduxredux-thunk

Error: Actions must be plain objects. Use custom middleware for async actions.


I'm currently witting an application using reactjs utilizing react-router, redux, thunk and firebase among other technologies. Unfortunately I'm getting the following error when I dispatch an actions which is asynchronous.

I went through all the similar questions suggested by stackoverflow and still can't find the problem.

Any help would be appreciated.

Error:

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
...

Solution

  • Thanks everybody for all your help. I did find the issue and am kicking myself. The code above works fine, the problem is where I registered the store. I was importing an auxiliary store file which I was using for testing purposes. Once I updated the real store, I forgot to switch to it.