[英]Fetch inside map in react
我正在尝试通过其campaignId 获取每个广告系列的统计信息。 从第一个 api 调用中提取活动 ID,然后在迭代时将该 ID 传递给每个活动统计信息的下一个 api 调用。 第一个 api 调用给出了正确的结果,但是在从第二个 api 调用迭代和获取它时,它会抛出和错误
未处理的拒绝(TypeError):ids.map 不是 function
export const loadStats = async () => {
const ids = await (await fetch('https://api.truepush.com/api/v1/listCampaign/1', {
method: "GET",
headers: {
Authorization: `${TOKEN}`,
"Content-Type": "application/json"
}
})).json()
const data = Promise.all(
ids.map(async (i) => await (await fetch(`https://api.truepush.com/api/v1/campaignStats/${i.data.campaignId}`, {
method: "GET",
headers: {
Authorization:`${TOKEN}`,
"Content-Type": "application/json"
}
})).json())
)
return data
};
我在迭代时期待所有这些统计数据:
https://i.stack.imgur.com/8kjwy.png
https://i.stack.imgur.com/VJni7.png ( listCampaign/1
的结果)
尝试这个:
export const loadStats = async () => {
const ids = await (await fetch('https://api.truepush.com/api/v1/listCampaign/1', {
method: "GET",
headers: {
Authorization: `${TOKEN}`,
"Content-Type": "application/json"
}
})).json()
const data = Promise.all(
ids.data.map(async (i) => await (await fetch(`https://api.truepush.com/api/v1/campaignStats/${i.campaignId}`, {
method: "GET",
headers: {
Authorization:`${TOKEN}`,
"Content-Type": "application/json"
}
})).json())
)
return data
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.