React Redux未处理的拒绝(TypeError):无法读取未定义的属性“ then”

时间:2019-11-28 01:44:07

标签: javascript reactjs redux react-redux redux-thunk

我的react组件中有一个redux函数调用,如下所示。

componentDidMount() {
    let gameId = this.props.match.params.gameId;

    this.props.fetchCurrentGameSession(gameId)
        .then(response => {
            if (response !== undefined) {
                if (!response.error) {
                    this.setState({
                        currentSession: {
                            loading: false,
                            loaded: true,
                            error: false,
                        }
                    });
                }
                else {
                    this.setState({
                        currentSession: {
                            loading: false,
                            loaded: false,
                            error: true,
                        }
                    });
                }
            }
        });

函数的声明:

const mapDispatchToProps = (dispatch) => {
    return {
        fetchCurrentGameSession: (gameId) => dispatch(loadCurrentGameSession(gameId)),
    }
};


export default compose(
    connect(
        mapStateToProps,
        mapDispatchToProps
    ),
    withStyles(styles, {
            withTheme: true
        }
    ))(LayoutGameView);

但是有时候,当页面加载时,我得到了错误

  

未处理的拒绝(TypeError):无法读取未定义的属性'then'

我将在哪里添加该函数以根据redux函数返回的内容来更改状态,以免出现此错误?

编辑:

action.js

export const FETCH_CURRENT_GAME_SESSION = '@@game/FETCH_CURRENT_GAME_SESSION';
export const FETCH_CURRENT_GAME_SESSION_SUCCESS = '@@game/FETCH_CURRENT_GAME_SESSION_SUCCESS';
export const FETCH_CURRENT_GAME_SESSION_FAILURE = '@@game/FETCH_CURRENT_GAME_SESSION_FAILURE';
export const loadCurrentGameSession = (gameId) => ({
    [RSAA]: {
        endpoint: `/api/games/${gameId}/sessions/current`,
        method: 'GET',
        headers: withAuth({ 'Content-Type': 'application/json' }),
        types: [
            FETCH_CURRENT_GAME_SESSION, FETCH_CURRENT_GAME_SESSION_SUCCESS, FETCH_CURRENT_GAME_SESSION_FAILURE
        ]
    }
});

0 个答案:

没有答案