I don't understand the error: this.state.categories.map is not a function ReactJS












-1















I'm fairly new to React JS and I'm getting the error that I can't use the map function on a state array i'm using.



I'm currently building a webapp using the Eventbrite api, getting data from the api is not the problem. But when I want to loop through the given data React responds with the error I have given above.



import React, { Component } from 'react'
import axios from 'axios'


export default class Categories extends Component {
state={
apiUrl : 'https://www.eventbriteapi.com/v3/categories/103',
OAuthToken : 'my OAuthToken',
loading: true,
categories:
}

componentWillMount(){
axios.get(`${this.state.apiUrl}?token=${this.state.OAuthToken}`)
.then(res => {
let result = res.data;
this.setState({categories : result});
console.log(this.state.categories);
if(this.state.result !== ''){
this.setState({loading:false});
}
})
.catch((error)=>{
console.log(error);
})
}

renderCategories(){
if(this.state.loading === false){
return(
<ul>
{this.state.categories.map(item=> (
<li key={item}>{item}</li>
))}
</ul>
)

}
}

render() {
return (
<div>
{this.renderCategories()}
</div>
)
}
}


I'm a little confused in what I'm doing wrong, any ideas?










share|improve this question























  • Are you sure that result coming back from your network call is actually an array?

    – Matthew Herbst
    Jan 2 at 23:47











  • What does the console.log() tell you about the updated value of the categories property?

    – Pointy
    Jan 2 at 23:47











  • res.data is possibly not an array?

    – Emile Bergeron
    Jan 2 at 23:47











  • Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

    – Joey Deckers
    Jan 3 at 0:18
















-1















I'm fairly new to React JS and I'm getting the error that I can't use the map function on a state array i'm using.



I'm currently building a webapp using the Eventbrite api, getting data from the api is not the problem. But when I want to loop through the given data React responds with the error I have given above.



import React, { Component } from 'react'
import axios from 'axios'


export default class Categories extends Component {
state={
apiUrl : 'https://www.eventbriteapi.com/v3/categories/103',
OAuthToken : 'my OAuthToken',
loading: true,
categories:
}

componentWillMount(){
axios.get(`${this.state.apiUrl}?token=${this.state.OAuthToken}`)
.then(res => {
let result = res.data;
this.setState({categories : result});
console.log(this.state.categories);
if(this.state.result !== ''){
this.setState({loading:false});
}
})
.catch((error)=>{
console.log(error);
})
}

renderCategories(){
if(this.state.loading === false){
return(
<ul>
{this.state.categories.map(item=> (
<li key={item}>{item}</li>
))}
</ul>
)

}
}

render() {
return (
<div>
{this.renderCategories()}
</div>
)
}
}


I'm a little confused in what I'm doing wrong, any ideas?










share|improve this question























  • Are you sure that result coming back from your network call is actually an array?

    – Matthew Herbst
    Jan 2 at 23:47











  • What does the console.log() tell you about the updated value of the categories property?

    – Pointy
    Jan 2 at 23:47











  • res.data is possibly not an array?

    – Emile Bergeron
    Jan 2 at 23:47











  • Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

    – Joey Deckers
    Jan 3 at 0:18














-1












-1








-1








I'm fairly new to React JS and I'm getting the error that I can't use the map function on a state array i'm using.



I'm currently building a webapp using the Eventbrite api, getting data from the api is not the problem. But when I want to loop through the given data React responds with the error I have given above.



import React, { Component } from 'react'
import axios from 'axios'


export default class Categories extends Component {
state={
apiUrl : 'https://www.eventbriteapi.com/v3/categories/103',
OAuthToken : 'my OAuthToken',
loading: true,
categories:
}

componentWillMount(){
axios.get(`${this.state.apiUrl}?token=${this.state.OAuthToken}`)
.then(res => {
let result = res.data;
this.setState({categories : result});
console.log(this.state.categories);
if(this.state.result !== ''){
this.setState({loading:false});
}
})
.catch((error)=>{
console.log(error);
})
}

renderCategories(){
if(this.state.loading === false){
return(
<ul>
{this.state.categories.map(item=> (
<li key={item}>{item}</li>
))}
</ul>
)

}
}

render() {
return (
<div>
{this.renderCategories()}
</div>
)
}
}


I'm a little confused in what I'm doing wrong, any ideas?










share|improve this question














I'm fairly new to React JS and I'm getting the error that I can't use the map function on a state array i'm using.



I'm currently building a webapp using the Eventbrite api, getting data from the api is not the problem. But when I want to loop through the given data React responds with the error I have given above.



import React, { Component } from 'react'
import axios from 'axios'


export default class Categories extends Component {
state={
apiUrl : 'https://www.eventbriteapi.com/v3/categories/103',
OAuthToken : 'my OAuthToken',
loading: true,
categories:
}

componentWillMount(){
axios.get(`${this.state.apiUrl}?token=${this.state.OAuthToken}`)
.then(res => {
let result = res.data;
this.setState({categories : result});
console.log(this.state.categories);
if(this.state.result !== ''){
this.setState({loading:false});
}
})
.catch((error)=>{
console.log(error);
})
}

renderCategories(){
if(this.state.loading === false){
return(
<ul>
{this.state.categories.map(item=> (
<li key={item}>{item}</li>
))}
</ul>
)

}
}

render() {
return (
<div>
{this.renderCategories()}
</div>
)
}
}


I'm a little confused in what I'm doing wrong, any ideas?







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 23:42









Joey DeckersJoey Deckers

1




1













  • Are you sure that result coming back from your network call is actually an array?

    – Matthew Herbst
    Jan 2 at 23:47











  • What does the console.log() tell you about the updated value of the categories property?

    – Pointy
    Jan 2 at 23:47











  • res.data is possibly not an array?

    – Emile Bergeron
    Jan 2 at 23:47











  • Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

    – Joey Deckers
    Jan 3 at 0:18



















  • Are you sure that result coming back from your network call is actually an array?

    – Matthew Herbst
    Jan 2 at 23:47











  • What does the console.log() tell you about the updated value of the categories property?

    – Pointy
    Jan 2 at 23:47











  • res.data is possibly not an array?

    – Emile Bergeron
    Jan 2 at 23:47











  • Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

    – Joey Deckers
    Jan 3 at 0:18

















Are you sure that result coming back from your network call is actually an array?

– Matthew Herbst
Jan 2 at 23:47





Are you sure that result coming back from your network call is actually an array?

– Matthew Herbst
Jan 2 at 23:47













What does the console.log() tell you about the updated value of the categories property?

– Pointy
Jan 2 at 23:47





What does the console.log() tell you about the updated value of the categories property?

– Pointy
Jan 2 at 23:47













res.data is possibly not an array?

– Emile Bergeron
Jan 2 at 23:47





res.data is possibly not an array?

– Emile Bergeron
Jan 2 at 23:47













Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

– Joey Deckers
Jan 3 at 0:18





Ah the problem was that it isn't an array. Stupid me got clumsy. Thanks for the assistance!

– Joey Deckers
Jan 3 at 0:18












2 Answers
2






active

oldest

votes


















0














The .map function only works on array-like variables. If that's the actual endpoint that you're calling then from the documentation it looks like it's not returning an array but an Object in the following format:



{
"id": "103",
"resource_uri": "https://www.eventbriteapi.com/v3/categories/103/",
"name": "Music",
"name_localized": "Music",
"short_name": "Music",
"short_name_localized": "Music",
"subcategories": [
{
"id": "3003",
"resource_uri": "https://www.eventbriteapi.com/v3/subcategories/3003/",
"name": "Classical",
"parent_category": {}
}
]
}


If you want to iterate through the keys or values you can turn that Object into an array using Object.keys(result) or Object.values(result)






share|improve this answer































    0














    Here is how I do this with fetch()...



    fetch('https://www.eventbriteapi.com/v3/organizers/{organizer_id}/events/?status=live&token=******')
    .then(response => response.json())
    .then(data => gatherData(data.events))

    function gatherData(data) {
    const options = data.map(item => `
    <div id="event-contain">
    <div class="event-left" style="width: 66%;">
    <img src="${item.logo.url}">
    <a href="${item.url}"><h5>${item.name.html}</h5></a><br/>
    <p>${item.description.text}</p>
    <a href="${item.url}"><button style="margin-bottom: 10px; padding: 15px;">Buy Tickets</button></a><br/><br/>
    <div class="spacer" style="width: 100%; height: 30px; border-top: 1px solid #e4e4e4;"></div>
    </div>
    </div>
    `).join('');
    evdis.innerHTML = options;
    }


    It seems like you are missing a step in setting a variable to the JSON response. You should then be able to enter into the highest state of the JSON response ( aka events ) This will allow you to iterate through all of the events that you own using .map and display those events.



    I hope this helps.
    Best of luck!






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014627%2fi-dont-understand-the-error-this-state-categories-map-is-not-a-function-reactj%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The .map function only works on array-like variables. If that's the actual endpoint that you're calling then from the documentation it looks like it's not returning an array but an Object in the following format:



      {
      "id": "103",
      "resource_uri": "https://www.eventbriteapi.com/v3/categories/103/",
      "name": "Music",
      "name_localized": "Music",
      "short_name": "Music",
      "short_name_localized": "Music",
      "subcategories": [
      {
      "id": "3003",
      "resource_uri": "https://www.eventbriteapi.com/v3/subcategories/3003/",
      "name": "Classical",
      "parent_category": {}
      }
      ]
      }


      If you want to iterate through the keys or values you can turn that Object into an array using Object.keys(result) or Object.values(result)






      share|improve this answer




























        0














        The .map function only works on array-like variables. If that's the actual endpoint that you're calling then from the documentation it looks like it's not returning an array but an Object in the following format:



        {
        "id": "103",
        "resource_uri": "https://www.eventbriteapi.com/v3/categories/103/",
        "name": "Music",
        "name_localized": "Music",
        "short_name": "Music",
        "short_name_localized": "Music",
        "subcategories": [
        {
        "id": "3003",
        "resource_uri": "https://www.eventbriteapi.com/v3/subcategories/3003/",
        "name": "Classical",
        "parent_category": {}
        }
        ]
        }


        If you want to iterate through the keys or values you can turn that Object into an array using Object.keys(result) or Object.values(result)






        share|improve this answer


























          0












          0








          0







          The .map function only works on array-like variables. If that's the actual endpoint that you're calling then from the documentation it looks like it's not returning an array but an Object in the following format:



          {
          "id": "103",
          "resource_uri": "https://www.eventbriteapi.com/v3/categories/103/",
          "name": "Music",
          "name_localized": "Music",
          "short_name": "Music",
          "short_name_localized": "Music",
          "subcategories": [
          {
          "id": "3003",
          "resource_uri": "https://www.eventbriteapi.com/v3/subcategories/3003/",
          "name": "Classical",
          "parent_category": {}
          }
          ]
          }


          If you want to iterate through the keys or values you can turn that Object into an array using Object.keys(result) or Object.values(result)






          share|improve this answer













          The .map function only works on array-like variables. If that's the actual endpoint that you're calling then from the documentation it looks like it's not returning an array but an Object in the following format:



          {
          "id": "103",
          "resource_uri": "https://www.eventbriteapi.com/v3/categories/103/",
          "name": "Music",
          "name_localized": "Music",
          "short_name": "Music",
          "short_name_localized": "Music",
          "subcategories": [
          {
          "id": "3003",
          "resource_uri": "https://www.eventbriteapi.com/v3/subcategories/3003/",
          "name": "Classical",
          "parent_category": {}
          }
          ]
          }


          If you want to iterate through the keys or values you can turn that Object into an array using Object.keys(result) or Object.values(result)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 0:22









          dandan

          846620




          846620

























              0














              Here is how I do this with fetch()...



              fetch('https://www.eventbriteapi.com/v3/organizers/{organizer_id}/events/?status=live&token=******')
              .then(response => response.json())
              .then(data => gatherData(data.events))

              function gatherData(data) {
              const options = data.map(item => `
              <div id="event-contain">
              <div class="event-left" style="width: 66%;">
              <img src="${item.logo.url}">
              <a href="${item.url}"><h5>${item.name.html}</h5></a><br/>
              <p>${item.description.text}</p>
              <a href="${item.url}"><button style="margin-bottom: 10px; padding: 15px;">Buy Tickets</button></a><br/><br/>
              <div class="spacer" style="width: 100%; height: 30px; border-top: 1px solid #e4e4e4;"></div>
              </div>
              </div>
              `).join('');
              evdis.innerHTML = options;
              }


              It seems like you are missing a step in setting a variable to the JSON response. You should then be able to enter into the highest state of the JSON response ( aka events ) This will allow you to iterate through all of the events that you own using .map and display those events.



              I hope this helps.
              Best of luck!






              share|improve this answer






























                0














                Here is how I do this with fetch()...



                fetch('https://www.eventbriteapi.com/v3/organizers/{organizer_id}/events/?status=live&token=******')
                .then(response => response.json())
                .then(data => gatherData(data.events))

                function gatherData(data) {
                const options = data.map(item => `
                <div id="event-contain">
                <div class="event-left" style="width: 66%;">
                <img src="${item.logo.url}">
                <a href="${item.url}"><h5>${item.name.html}</h5></a><br/>
                <p>${item.description.text}</p>
                <a href="${item.url}"><button style="margin-bottom: 10px; padding: 15px;">Buy Tickets</button></a><br/><br/>
                <div class="spacer" style="width: 100%; height: 30px; border-top: 1px solid #e4e4e4;"></div>
                </div>
                </div>
                `).join('');
                evdis.innerHTML = options;
                }


                It seems like you are missing a step in setting a variable to the JSON response. You should then be able to enter into the highest state of the JSON response ( aka events ) This will allow you to iterate through all of the events that you own using .map and display those events.



                I hope this helps.
                Best of luck!






                share|improve this answer




























                  0












                  0








                  0







                  Here is how I do this with fetch()...



                  fetch('https://www.eventbriteapi.com/v3/organizers/{organizer_id}/events/?status=live&token=******')
                  .then(response => response.json())
                  .then(data => gatherData(data.events))

                  function gatherData(data) {
                  const options = data.map(item => `
                  <div id="event-contain">
                  <div class="event-left" style="width: 66%;">
                  <img src="${item.logo.url}">
                  <a href="${item.url}"><h5>${item.name.html}</h5></a><br/>
                  <p>${item.description.text}</p>
                  <a href="${item.url}"><button style="margin-bottom: 10px; padding: 15px;">Buy Tickets</button></a><br/><br/>
                  <div class="spacer" style="width: 100%; height: 30px; border-top: 1px solid #e4e4e4;"></div>
                  </div>
                  </div>
                  `).join('');
                  evdis.innerHTML = options;
                  }


                  It seems like you are missing a step in setting a variable to the JSON response. You should then be able to enter into the highest state of the JSON response ( aka events ) This will allow you to iterate through all of the events that you own using .map and display those events.



                  I hope this helps.
                  Best of luck!






                  share|improve this answer















                  Here is how I do this with fetch()...



                  fetch('https://www.eventbriteapi.com/v3/organizers/{organizer_id}/events/?status=live&token=******')
                  .then(response => response.json())
                  .then(data => gatherData(data.events))

                  function gatherData(data) {
                  const options = data.map(item => `
                  <div id="event-contain">
                  <div class="event-left" style="width: 66%;">
                  <img src="${item.logo.url}">
                  <a href="${item.url}"><h5>${item.name.html}</h5></a><br/>
                  <p>${item.description.text}</p>
                  <a href="${item.url}"><button style="margin-bottom: 10px; padding: 15px;">Buy Tickets</button></a><br/><br/>
                  <div class="spacer" style="width: 100%; height: 30px; border-top: 1px solid #e4e4e4;"></div>
                  </div>
                  </div>
                  `).join('');
                  evdis.innerHTML = options;
                  }


                  It seems like you are missing a step in setting a variable to the JSON response. You should then be able to enter into the highest state of the JSON response ( aka events ) This will allow you to iterate through all of the events that you own using .map and display those events.



                  I hope this helps.
                  Best of luck!







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 8 at 16:07

























                  answered Jan 8 at 16:00









                  Nathan TiniusNathan Tinius

                  1064




                  1064






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014627%2fi-dont-understand-the-error-this-state-categories-map-is-not-a-function-reactj%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith