0
votes

Previously, I made a lot of successfuly calls to another APIs in my backend (springboot), using this same way, but creating a new call I received the error: "Actions must be plain objects. Use custom middleware for async actions". I tried a lot of answers of other people but no luck. Does anyone knows waht happens? thanks a lot in advance.

Action:

export function getAllValuesAction(listId, player) {
    return async (dispatch) => {
        dispatch( getAllValues() );
        try {
            const token = localStorage.getItem('token');
            if(token) {
                tokenAuth(token);
            }
            const answer = await clientAxios.post(`/pays/sum/player/${listId}`, player);
            dispatch( obtenerSumaPagosJugListaExito(answer.data) );
        } catch (error) {
            dispatch( getAllValuesError() );
        }
    }
}

const getAllValues = () => ({
    type: START_GETTING_VALUES,
    payload: true
});
const getAllValuesSuccess = sumaPago => ({
    type: GETTING_VALUES_SUCCESS,
    payload: answer
});
const getAllValuesError = sumaPago => ({
    type: GETTING_VALUES_ERROR
});

The store:

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducers';
const store = createStore(
    reducer, 
    compose( applyMiddleware(thunk), 
        typeof window === 'object' &&
            typeof window.__REDUX_DEVTOOLS_EXTENSION__ !== 'undefined' ? 
                window.__REDUX_DEVTOOLS_EXTENSION__() : f => f
    )
);
export default store;

tokenAuth:

import clientAxios from './axios';
const tokenAuth = token => {
    if(token) {
          clienteAxios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
    } else {
        delete clienteAxios.defaults.headers.common['Authorization'];
    }
}
export default tokenAuth;

const clientAxios = axios.create({
    baseURL: process.env.REACT_APP_BACKEND_URL
});
export default clientAxios;

Plus:

just if someone also knows (but at least I need to know just the first "react redux error" question): when I use GET in the call I receive the error message without having response. When I use POST (like example in "Controller" below), I receive first the response and then, the error message.

The springboot controller receiving the request:

@PostMapping("/pays/sum/player/{listId}")
public Double getSumPayPlayerList(@RequestHeader (name="Authorization") String token, @PathVariable long listId, @RequestBody Player player ) {...}

the Pay repository making the query to the database:

@Query("select SUM(g.import) from Pay g where g.listId=?1 and g.player=?2")
public Double sumPaysByPlayerList(Long listId, Player player);

Thanks !

1
In an async action, you are supposed to dispatch only once. But you are dispatching multiple actions there. You may consider splitting your getAllValuesAction into smaller, individual actions. - skmak
Thank you Skmak, I tried a lot of things as you say (like example below), but nothing seems to work: - Diego Benedetto
export const getAllValuesAction = (listId, player) => { return async (dispatch) => { const token = localStorage.getItem('token'); if(token) { tokenAuth(token); } const answer = await clientAxios.post(/pagos/sum/jug/${listId}, player); const data = answer.data; dispatch ({ type: GETTING_VALUES_SUCCESS, payload: data }); } } - Diego Benedetto
Can you go ahead and upgrade to the latest version of Redux (4.1), or even better, switch to using Redux Toolkit's configureStore? Those won't immediately fix the issue, but they have improved error messages that will tell you what is being passed to dispatch instead of a plain action. - markerikson
Thank you markerikson, I think I already have that version, this is a short part of the package.json: "react-redux": "^7.2.4", "redux": "^4.1.0", "redux-thunk": "^2.3.0", - Diego Benedetto

1 Answers

0
votes

I found that the issue was because I was trying to show that information inside a Modal. For some reason that situation generates some delay or desynchronization, so had to remove the modal and find another way to show the info. Thank you all for your help.