I'm beginner in redux and ReactJs. I have issue with when i trying to call login api the response not getting.
import { CALL_API } from 'redux-api-middleware';
export function login(toSend) {
return function(dispatch, getState) {
dispatch(mopetsLogin(toSend)).then(() =>
{
console.log(getState().mopets_api.token.access_token);
dispatch(mopetsMe(getState().mopets_api.token.access_token));
});
}
}
export function mopetsLogin(toSend) {
console.log('toSend');
console.log(toSend);
return {
[CALL_API]: {
endpoint: 'http://api.mopets.com/app_dev.php/login',
method: 'POST',
/*headers: { 'Content-Type': 'application/json' },*/
body: JSON.stringify(toSend),
types: [
'INITIAL_LOGIN_MOPETS_REQUEST',
{
type: 'INITIAL_LOGIN_MOPETS_SUCCESS',
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type');
if (contentType && ~contentType.indexOf('json')) {
// Just making sure res.json() does not raise an error
const contentType = res.headers.get('Content-Type');
if (contentType && ~contentType.indexOf('json')) {
// Just making sure res.json() does not raise an error
return res.json().then((json) => {
var o = new Object();
o["token"] = json;
return o;
}
)
}
;
}
}
},
'INITIAL_LOGIN_MOPETS_FAILURE'
]
}
}
}
main.bundle.js:42605 Uncaught (in promise) TypeError: Cannot read property 'access_token' of undefined
this type error showing can please look the code and provide some information to solve this.
This appens firs because your
getState().mopets_api.token
is undefined.
Maybe it is only a path problem, try to console.log your res to check if the object gets correctly populated.