Javascript React-Redux未处理拒绝(TypeError):无法读取属性';然后';未定义的
我在react组件中有一个redux函数调用,如下所示Javascript React-Redux未处理拒绝(TypeError):无法读取属性';然后';未定义的,javascript,reactjs,redux,react-redux,redux-thunk,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.
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
]
}
});
确保
loadCurrentGameSession
返回承诺。只有Promise可以使用。那么
@jdn在这种格式下我该怎么做呢?我正在搜索,没有一个示例的操作格式与我的类似,因为我使用的是RSAA中间件。我在帖子中添加了我的loadCurrentGameSession
,以供查看。根据github的说法,中间件包已经有2年的历史了。为项目选择依赖项时,请避免放弃和未使用的包。用于在npm中搜索以筛选出放弃的包。@cclloyd Hope可以帮助您。正如jstice4all所说,您应该避免放弃和未使用的包。如果可以,尝试为您的项目使用另一个中间件以获得更多支持。