Diego Rafael Harari
Diego Rafael Harari

Reputation: 3

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

Upvotes: 0

Views: 77

Answers (2)

Beginner
Beginner

Reputation: 9135

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>

Upvotes: 1

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 20007

Here's a simple example accessing the players to help get you started.

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)
})();

Upvotes: 0

Related Questions