Javascript 它作为组合值4,5,6传递 我是新来的 我需要进行两个不同的api调用 从第一个api调用的结果中,我得到变量firstAPIid中的id, 我需要将这个id firstAPID传递给第二个api调用 但问题是它作为组合值4,5,6传递 从第二个api调用中,我需要检索电子邮件并将其显示在浏览器中 我是否需要使用promise、async或redux本身才能实现它
我研究并参考了下面的链接,但仍然没有运气Javascript 它作为组合值4,5,6传递 我是新来的 我需要进行两个不同的api调用 从第一个api调用的结果中,我得到变量firstAPIid中的id, 我需要将这个id firstAPID传递给第二个api调用 但问题是它作为组合值4,5,6传递 从第二个api调用中,我需要检索电子邮件并将其显示在浏览器中 我是否需要使用promise、async或redux本身才能实现它,javascript,html,arrays,reactjs,redux,Javascript,Html,Arrays,Reactjs,Redux,我研究并参考了下面的链接,但仍然没有运气 你能告诉我怎么修吗 下面提供我的代码片段和沙盒 { store.dispatch(dispatchFunc=>{ dispatchFunc({type:“FETCH_DATA_START”}); axios .get(“https://reqres.in/api/users?page=2") //axios.get()https://jsonplaceholder.typicode.com/posts') 。然后(响应=>{ log(“respon
{
store.dispatch(dispatchFunc=>{
dispatchFunc({type:“FETCH_DATA_START”});
axios
.get(“https://reqres.in/api/users?page=2")
//axios.get()https://jsonplaceholder.typicode.com/posts')
。然后(响应=>{
log(“response.data.data-->”,response.data.data);
console.log(
“response.data.data[0].id-->”,
response.data.data[0].id
);
调度函数({
类型:“接收的_数据”,
有效载荷:response.data.data
});
让firstapid=response.data.data.map(obj=>{
返回obj.id;
});
log(“firstapid-->”,firstapid);
返回新承诺((解决、拒绝)=>{
//变量url=`https://jsonplaceholder.typicode.com/comments?postId=3`;
变量url=
`https://jsonplaceholder.typicode.com/comments?postId=` +
第一类;
//response.data.data[0].id;
log(“第二个url-->”,url);
axios
.get(url)
。然后(响应=>{
var lFilterData=“”;
//memberGroupingHelper.filterData(response.data,additionalParams);
解析(lFilterData);
})
.catch(错误=>{
if(error.response){
console.log(
`@@@@@@@@@@@@@@Helpeeer拒绝的服务错误`
);
}
拒绝(“”);
});
});
})
.catch(错误=>{
dispatchFunc({type:“FETCH_DATA_ERROR”,有效负载:err});
});
});
}}
/>
我发现了您的问题。这是因为你没有处理承诺的结果。为此,只需添加.then()
和.catch()
函数:
<FetchButton
onFetchClick={() => {
store.dispatch(dispatchFunc => {
dispatchFunc({ type: "FETCH_DATA_START" });
axios
.get("https://reqres.in/api/users?page=2")
// axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
console.log("response.data.data---->", response.data.data);
console.log(
"response.data.data[0].id---->",
response.data.data[0].id
);
dispatchFunc({
type: "RECEIVED_DATA",
payload: response.data.data
});
let firstAPIid = response.data.data.map(obj => {
return obj.id;
});
console.log("firstAPIid---->", firstAPIid);
return new Promise((resolve, reject) => {
//var url = `https://jsonplaceholder.typicode.com/comments?postId=3`;
var url =
`https://jsonplaceholder.typicode.com/comments?postId=` +
firstAPIid;
//response.data.data[0].id;
console.log("second url---->", url);
axios
.get(url)
.then(response => {
var lFilterData = "";
//memberGroupingHelper.filterData(response.data, additionalParams);
resolve(lFilterData);
})
.catch(error => {
if (error.response) {
console.log(
`@@@@@@@@@@@@@@ service error from helpeeeeeer reject`
);
}
reject("");
});
}).then((previousResponse) => {
//Here you resolved the promise with the resolve value above
console.log(previousResponse)
}).catch((error) => {
//Here you resolved the promise with the reject value above
console.log(error);
});
})
.catch(err => {
dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err });
});
});
}}
/>
嘿,我运行了你的更新代码,但我还是遇到了一些问题,应该像这样打印
第二个url-->https://jsonplaceholder.typicode.com/comments?postId=4
但仍作为组合值打印第二个url-->https://jsonplaceholder.typicode.com/comments?postId=4,5,6
你能在我的沙箱中更新吗?你想打印第二个沙箱的响应吗API调用?或者你想在屏幕上显示什么?我需要将这个FirstAPI从第一个api传递到第二个api,并在浏览器中打印响应哪个响应?是的,这是我们需要修复的错误,而不是作为4,5,6传递,我们需要像这样分别传递4和5
<FetchButton
onFetchClick={() => {
store.dispatch(dispatchFunc => {
dispatchFunc({ type: "FETCH_DATA_START" });
axios
.get("https://reqres.in/api/users?page=2")
// axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
console.log("response.data.data---->", response.data.data);
console.log(
"response.data.data[0].id---->",
response.data.data[0].id
);
//First of all we'll create the number of requestes base on the previous Response
const promises = response.data.data.reduce((previousValue, { id }) => {
previousValue.push(axios.get(`https://jsonplaceholder.typicode.com/comments?postId=${id}`));
return previousValue;
},[]);
//We use the built in function to fetch the data
axios.all(promises)
.then((responses) => {
//Here you have all responses processed
const emailsMapped = responses.reduce((previousValue, { data }) => {
const emails = data.map(({ email }) => email)
previousValue.push(...emails);
return previousValue;
}, [])
//You send the emails you want
dispatchFunc({
type: "RECEIVED_DATA",
payload: emailsMapped
});
console.log(emailsMapped);
})
})
.catch(err => {
dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err });
});
});
}}
/>
listItems.push(<div key={fetchedDataId++}>{elem}</div>);