Error with Promise fetch

Asked

Viewed 24 times

0

Guys I’m doing a beginner project with API,but arrives at a part I define a function but somehow error :

script.js:29 Uncaught (in promise) TypeError: Cannot read property 'querySelector' of undefined
    at criar (script.js:29)
    at script.js:10

the complete code :

let cont = document.querySelector(".sea");

function gerar (id , num ) {
    fetch(`http://api.football-data.org/v2/players/${id}/matches` , {
    "headers":  {
        "X-Auth-Token":"dc27d36b147a479bae295e494c925263"

    }
}).then( r => r.json()).then(r => criar(r,num));

}


function jogadores() {
    
    let primeiro = Math.round(Math.random() * 50)
    let segundo  = Math.round(Math.random() * 50)
    
    gerar (primeiro,1);
    gerar (segundo,2);
    
}



function criar(r,num) {

    let play = cont.document.querySelector(`.box${num}`)


        
}

jogadores()
  • takes the cont. of cont.document.querySelector('.box${num}')

  • this container. makes reference to sea which is the container of two Divs that I will select ...

1 answer

0

Its variable cont is already an element, so you don’t need the .document.

You can use the .querySelector direct in the cont variable:

let play = cont.querySelector(`.box${num}`)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.