Jump to content

Recommended Posts

Posted

Olá pessoal, no meu código abaixo estou usando Fetch api para retornar os dados do usuario (Github) objetivo é pegar o número total de estrelas (starsgazers) de todos os repositórios.

Eu utilizei um foreach para percorrer todos os repositórios e retornar suas respectivas "stars earned" eu tenho esse valor mas no meu código abaixo eu só estou  tendo esse valor total ao clicar pela segunda vez no botão que chama a função. O código está funcional mas vejo que a lógica não.  agradeço alguma dica que puderem dar.

 

 

const btnEl = document.getElementById("btnUser");
function run() {
  let stars = 0;
  userEl = document.getElementById("gituser").value;
  let api = `https://api.github.com/users/${userEl}/repos`;
  fetch(api)
    .then((data) => data.json())
    .then((data) => {
      console.log(data);
      const repo = data.map(({ name }) => name);
      console.log(repo);
      console.log(repo.length);
      repo.forEach(function (item) {
        console.log(item);
        let repoName = `https://api.github.com/repos/${userEl}/`;
        fetch(repoName + item)
          .then((repores) => repores.json())
          .then((repores) => {
            
            console.log(repores.stargazers_count)
            stars += repores.stargazers_count;
          });
      });
      console.log("Stars number is: ", stars);
       
  
    });
}
btnEl.addEventListener("click", () => {
  run();
});
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.