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 !
asyncaction, you are supposed to dispatch only once. But you are dispatching multiple actions there. You may consider splitting yourgetAllValuesActioninto smaller, individual actions. - skmakconfigureStore? Those won't immediately fix the issue, but they have improved error messages that will tell you what is being passed todispatchinstead of a plain action. - markerikson