[英]How to call api within map function
嗨,我正在尝试在地图中进行 api 调用,但它只是作为 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>
)
}```
您正在使用map
创建一系列承诺。 使用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.