您好,我试图在地图内进行api调用,但是它只是作为object.promise返回。是否可以在其中调用api而不将其作为对象返回。承诺
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>
)
}```
答案 0 :(得分:0)
您正在使用map
创建承诺数组。使用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);
});