简体   繁体   中英

React-Redux action wont dispatch

I am developing react-native/expo app with node/express api and im using redux as state manager.When i try to dispatch redux action nothing happened,i do not get any errors but the action does not dispatch.It is not api error becouse when i test api with Postman everything works fine.

This is my login action code and i know that there is no problem with the action call in component becouse i console log params that i passed in component and i see correct data.

 export const loginUser = ({ email, password }) => async (dispatch) => { const config = { headers: { 'Content-Type': 'application/json', }, }; const body = JSON.stringify({ email, password }); console.log(body); try { const res = await axios.post( 'http://urlToMyServer/api/auth', body, config ); dispatch({ type: LOGIN_SUCCESS, payload: res.data, }); dispatch(loadUser()); } catch (err) { const errors = err.response.data.errors; if (errors) { errors.forEach((error) => dispatch(setAlert(error.msg, 'danger'))); } dispatch({ type: LOGIN_FAIL, }); } };
Here is what i get after i try to log in screenshot from console

And nothing happened in my react native debugger react native debugger screenshot

In my try block i also dispatch another action called loadUser() but there is no problems with it because even when i remove action call from loginUser it still does not work

After about 2-3 minutes after trying to dispatch action i get this error in console

*[Unhandled promise rejection: TypeError: Cannot read property 'data' of undefined] * store\actions\auth.js:66:50 in - node_modules\regenerator-runtime\runtime.js:45:39 in tryCatch - node_modules\regenerator-runtime\runtime.js:274:21 in invoke -... 8 more stack frames from framework internals*

Thanks in advance!

I am developing react-native/expo app with node/express api and im using redux as state manager.When i try to dispatch redux action nothing happened,i do not get any errors but the action does not dispatch.It is not api error becouse when i test api with Postman everything works fine.

This is my login action code and i know that there is no problem with the action call in component becouse i console log params that i passed in component and i see correct data.

 export const loginUser = ({ email, password }) => async (dispatch) => { const config = { headers: { 'Content-Type': 'application/json', }, }; const body = JSON.stringify({ email, password }); console.log(body); try { const res = await axios.post( 'http://urlToMyServer/api/auth', body, config ); dispatch({ type: LOGIN_SUCCESS, payload: res.data, }); dispatch(loadUser()); } catch (err) { const errors = err.response.data.errors; if (errors) { errors.forEach((error) => dispatch(setAlert(error.msg, 'danger'))); } dispatch({ type: LOGIN_FAIL, }); } };
Here is what i get after i try to log in screenshot from console

And nothing happened in my react native debugger react native debugger screenshot

In my try block i also dispatch another action called loadUser() but there is no problems with it because even when i remove action call from loginUser it still does not work

After about 2-3 minutes after trying to dispatch action i get this error in console

*[Unhandled promise rejection: TypeError: Cannot read property 'data' of undefined] * store\actions\auth.js:66:50 in - node_modules\regenerator-runtime\runtime.js:45:39 in tryCatch - node_modules\regenerator-runtime\runtime.js:274:21 in invoke -... 8 more stack frames from framework internals*

Thanks in advance!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM