[英]How to call api within map function
Hi im trying to do an api call within a map, however it just returns as an object.promise.嗨,我正在尝试在地图中进行 api 调用,但它只是作为 object.promise 返回。 Is there away to call api within it without it returning as an object.promise
是否可以在其中调用 api 而不将其作为 object.promise 返回
const MatchHistory = ({ history }) => {
const Match=[];
return (
<div>
<p>
{history.map( hist => (
axios.get('https://polar-hollows-37538.herokuapp.com/matchdata/'+hist.gameId).then(response => (Match.push(response.data.platformId))),
console.log('get:'+axios.get('https://polar-hollows-37538.herokuapp.com/matchdata/'+hist.gameId).then(response =>(Match.push(response.data.mapId)))),
<Card>
<img src={'img\\'+hist.lane+'.png'}></img>
<li key={hist.gameId}>
<h5 className="card-title1">Lane:{hist.lane}</h5>
<h5 className="card-title1">Role:{hist.role}</h5>
<h5 className="card-title1">Champion:{hist.champion}</h5>
<h5 className="card-title1">GameId:{hist.gameId}</h5>
</li>
</Card>
))}}
</p>
</div>
)
}```
You are creating array of promises using map
.您正在使用
map
创建一系列承诺。 Use Promise.all
to resolve the array of promises.使用
Promise.all
来解析Promise.all
数组。
const promises = history.map(hist => axios.get('https://polar-hollows-37538.herokuapp.com/matchdata/'+hist.gameId));
const Match = [];
Promise.all(promises)
.then(function(values) {
values.forEach(value => {
Match.push(value.data.platformId);
})
})
.catch(err => {
console.error(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.