Javascript 如何使用一个API中的ID从Redux中的另一个API获取信息?
因此,API链接www.API.com/id_列表为我提供了另一个带有链接www.API.com/{id}的API中某些信息的id 我正在使用Redux,我可以获得id列表,但是我很难通过id获得所需的信息。它一直告诉我它是未定义的 这是我的代码:Javascript 如何使用一个API中的ID从Redux中的另一个API获取信息?,javascript,reactjs,redux,Javascript,Reactjs,Redux,因此,API链接www.API.com/id_列表为我提供了另一个带有链接www.API.com/{id}的API中某些信息的id 我正在使用Redux,我可以获得id列表,但是我很难通过id获得所需的信息。它一直告诉我它是未定义的 这是我的代码: export const EVENT_DETAIL = 'EVENT_DETAIL'; export const eventDetail = (payload) => { return { type:
export const EVENT_DETAIL = 'EVENT_DETAIL';
export const eventDetail = (payload) => {
return {
type: EVENT_DETAIL,
data: payload.data,
index: payload.index
};
}
export const getDetail = (events, index) => {
return function(dispatch) {
const url = `${URL2}/${events}`;
axios.get(url)
.then (function (response) {
dispatch(eventDetail({
data: response.data.events,
index: index
}))
})
.catch(function(error) {
console.log(error);
})
}
}
componentDidMount = () => {
this.props.getDetail();
console.log(this.props.getDetail())
}
你知道我为什么不能把信息显示出来吗
继续获取以下错误:
Error: Request failed with status code 404
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:78)
getDetail操作返回的函数本身不返回任何内容-这就是为什么undefined会记录到控制台中的原因。在axios.get函数前面放一个return,这将以承诺的形式满足您的请求
return axios.get(url)
.then (function (response) {
dispatch(eventDetail({
data: response.data.events,
index: index
}))
})
.catch(function(error) {
console.log(error);
})
}
要从请求中获得响应,您需要等待它得到解决。这是通过打电话来完成的。然后是承诺。你的组件应该是这样的
componentDidMount = () => {
this.props.getDetail()
.then(res => console.log(res))
}
不,这不起作用-仍然得到相同的错误没有看到你附加的图像。您将收到一个404未找到错误。这是由于您的网络请求无法找到您要查找的内容。你能记录下你的请求来源并确保它是正确的吗?