我是redux和ReactJs的初学者。当我尝试调用登录API时,响应出现问题,我遇到了问题。

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未捕获(承诺)TypeError:无法读取未定义的属性'access_token'


此类型错误显示
可以请看一下代码并提供一些信息来解决此问题。

最佳答案

这可能是因为您的

getState().mopets_api.token


未定义。

也许这只是一个路径问题,请尝试console.log您的res以检查对象是否正确填充。

关于javascript - 进入api时调用api响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47673441/

10-09 15:06
查看更多