【发布时间】:2020-03-24 04:01:30
【问题描述】:
我对 reactjs 很陌生,我正在尝试调用 api 数据以通过 axios 获取响应。但是一次又一次地得到同样的错误。
错误 未捕获(承诺中)类型错误:无法读取未定义的属性“数据”。
有很多类似的问题,但我找不到任何可以帮助我的答案。我使用的代码如下。
代码
const token = userService.getToken();
const api = `http://localhost:54729/api/Search?searchString=${text}`;
axios
.get(api, { headers: { Authorization: `Bearer ${token}` } })
.then(res => {
console.log("hello" + res);
try {
dispatch({
type: FETCH_PRODUCTS,
payload: res.data// Dummy data
});
} catch (err) {
console.log("error" + err);
console.log(res.data);
}
});
编辑
api 的响应是
[
{
"id": 0,
"title": "example-, example- und example(CC 11): example– example Berlin",
"description": null,
"owner": null,
"link": "/search/herz?url=https://www.example.de/example/example/example/",
"url": "https://www.example.de/example/charitecentren/example/",
"type": "External",
"rank": 0
},
{
"id": 0,
"title": "example Klinik mit exampleKardiologie (example) - Charité – example Berlin",
"description": null,
"owner": null,
"link": "/search/herz?url=https://example-cvk.example.de/",
"url": "https://example-example.example.de/",
"type": "External",
"rank": 0
},
{
"id": 0,
"title": "Deutsche Zentren example example: example– Universitätsmedizin example",
"description": null,
"owner": null,
"link": "/search/herz?url=https://www.example.de/forschung/example/example/",
"url": "https://www.example.de/example/example/deutsche_zentren_fuer_gesundheitsforschung/",
"type": "External",
"rank": 0
},
]
当我console.log(res.data) 它说undefined。
另外,到目前为止,还没有人问过我dispatch: FETCH_PRODUCTS 到底在做什么。你可以在下面看到它。可能会有所帮助,我正在尝试做的事情。
case FETCH_PRODUCTS:
console.log(action)
return {
...state,
products: action.payload,
loading: false,
totalRecords: action.payload.length,
};
【问题讨论】:
-
能否请您在此处附上 API 的回复?
-
当您安慰
res时,它显示了什么?可能res里面没有data属性。 -
我再次编辑了问题。
-
console.log(res)的结果是否也未定义? -
是的,它也是未定义的@octobus
标签: reactjs react-redux axios