Object array iteration with map returns Undefined

Asked

Viewed 181 times

0

I am consuming the Github API and I am browsing the repositories of a particular user, but when trying to access the properties it is returning undefined.

As you can see in the picture, mine result is returning all objects from the repositories, but while trying to traverse them using the rep which is being passed as parameter it returns only the first repository, but the idea would be to return all the repositories.

My function that traverses the repositories using the map():

  getRepos = type => {
    return e => {
      axios
        .get(`https://api.github.com/users/Giovanni001/${type}`)
        .then(result => {
          console.log("RESULT", result);
          this.setState({
            [type]: result.data.map(rep => {
            console.log("REP", rep);
              return {
                name: rep.data.name,
                link: rep.data.html_url
              };
            })
          });
        })
        .catch(err => {
          console.log("ERRO: ", err);
          return <h1>Ops, deu algo errado !</h1>;
        });
      };
    };

meu parametro <code>rep</code> trazendo apenas um repositório

MEU PARAMETRO <code>result</code> que está percorrendo os repositórios trazendo todos os repositórios

o erro undefined que está retornando ao tentar acessar a propriedade <code>name</code>

1 answer

2


The mistake is very clear there Young man, You can’t read the property name of indefinido, pq:

In his map() you’re already picking up the data from date

[type]: result.data.map(rep => { ...

And then when you do in the Return:

name: rep.data.name,     <= erro de acesso de propriedade
link: rep.data.html_url  <= erro de acesso de propriedade

Gives the error pq rep already has the data of result and date, to fix the error just directly access the property in the rep:

name: rep.name,     <= acesso da propriedade nome
link: rep.html_url  <= acesso da propriedade html-url
  • 1

    Hello, I had made a request in another function and I was taking the data using the date, I was so fixated on that q I did not realize that the date was already being passed in the loop, Thanks for the help !

  • This happens a lot during man development :)

Browser other questions tagged

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