[英]Returning promises in javascript
I need some help returning a promise. 我需要一些帮助以兑现诺言。 I don't get why I am not getting the players value
我不明白为什么我没有获得球员的价值
The code 编码
const playerSelectName = document.getElementById('sel1');
const playerSelectPosition = document.getElementById('sel2');
const playerSelectAge = document.getElementById('sel3');
const searchButton = document.getElementById('search-btn');
async function getPlayers() {
const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty');
const playersObject = await response.json();
return playersObject;
}
searchButton.addEventListener('click', async () => {
const players = await getPlayers();
let [ name, position, age ] = [ playerSelectName.value, playerSelectPosition.value, playerSelectAge.value ];
yersObject.filter((playerAge) => {});
});
I cant get to access the code inside my listener function of the value players 我无法访问值播放器的侦听器函数中的代码
the problem was in destructuring check the below snippet. 问题出在破坏结构中,请检查以下代码段。 I hope this will solve the issue .
我希望这能解决问题。 Also please update what you are trying to achieve in filter so i can add on the solution.
另外,请更新您要在过滤器中实现的功能,以便我可以添加解决方案。
I didnt understood what are you trying to do in destructure part and after that filter 我不明白您要在分解部分中以及过滤器之后要做什么
async function getPlayers() { const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty'); const playersObject = await response.json(); return playersObject; } const searchButton = document.getElementById('search-btn'); searchButton.addEventListener('click', async () => { const players = await getPlayers(); players.forEach(player => { const {name, nationality, position} = player console.log(name, nationality, position) }) });
<button id='search-btn'> Load Results </button>
Here's a simple example accessing the players
to help get you started. 这是访问
players
以帮助您入门的一个简单示例。
async function getPlayers() { const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty'); return response.json(); } (async () => { const players = await getPlayers(); console.log(players) })();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.