【发布时间】:2019-03-11 20:07:15
【问题描述】:
我正在使用带有 node.js 后端的 react-redux 开发一个 Web 应用程序。我正在努力添加一个页面以将游戏添加到个人资料中。运行此代码时出现错误。
这是我收到错误时它正在使用的操作
export const addGames = (gameData, history) => dispatch => {
axios
.post("/api/profile/games", gameData)
.then(res => history.push("/dashboard"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
页面本身加载得很好。只有在调用该操作时才会出错。出错时指向 catch 语句。
通过邮递员进行 API 调用按预期返回,并且不会出错。
我得到的错误是
×
Unhandled Rejection (TypeError): Cannot read property 'data' of undefined
eval
C:\dev\SocialNetworkTutorial\devConnector\client\src\actions\profileActions.js:48:14
45 | axios
46 | .post("/api/profile", profileData)
47 | .then(res => history.push("/dashboard"))
> 48 | .catch(err =>
| ^ 49 | dispatch({
50 | type: GET_ERRORS,
51 | payload: err.response.data
【问题讨论】:
-
那么实际的错误是什么?
-
编辑帖子以包含错误
-
为什么你期望
err有一个response字段?尝试将其注销并查看可用的内容。它是一个 Error 对象,所以它至少应该有一个message字段 -
尝试使用 JSON.stringify 注销 err 对象,以便您可以看到结构。该错误似乎表明该结构不是您所期望的(err.response.data)。 .catch(err => console.log(JSON.stringify(err)) )
标签: reactjs react-redux