React + NodeJs Fetch issue












1














I am trying to fetch the following results of my api (which is working well)
http://localhost:5000/api/continents



{"data":[{"continentId":3,"CName":"Atlantis"},{"continentId":2,"CName":"Devias"},{"continentId":1,"CName":"Lorencia"}]}


into a react component (a simple array to begin with).



Endpoint code extracted from server.js:



app.get('/api/continents', (req, res) => {
connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => {
if(err){
return res.send(err)
}
else{
return res.json({
data: results
})
}
});
});


Here is my continents.js code:



import React, { Component } from 'react';
import './continents.css';

class Continents extends Component {
constructor() {
super();
this.state = {
continents:
}
}

ComponentDidMount() {
fetch('http://localhost:5000/api/continents')
.then(res => res.json())
.then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
}

render() {
return (
<div>
<h2>Continents</h2>
</div>
);
}
}

export default Continents;


And here is the App.js code:



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Continents from './components/continents/continents';

class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Developed with NodeJS + React</h1>
</header>
<Continents />
</div>
);
}
}

export default App;


Issue:



continents array is empty. No data fetched. However, there are no errors. I would very much appreciate if someone could guide me in the right direction to solve this. Thank you very much.










share|improve this question





























    1














    I am trying to fetch the following results of my api (which is working well)
    http://localhost:5000/api/continents



    {"data":[{"continentId":3,"CName":"Atlantis"},{"continentId":2,"CName":"Devias"},{"continentId":1,"CName":"Lorencia"}]}


    into a react component (a simple array to begin with).



    Endpoint code extracted from server.js:



    app.get('/api/continents', (req, res) => {
    connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => {
    if(err){
    return res.send(err)
    }
    else{
    return res.json({
    data: results
    })
    }
    });
    });


    Here is my continents.js code:



    import React, { Component } from 'react';
    import './continents.css';

    class Continents extends Component {
    constructor() {
    super();
    this.state = {
    continents:
    }
    }

    ComponentDidMount() {
    fetch('http://localhost:5000/api/continents')
    .then(res => res.json())
    .then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
    }

    render() {
    return (
    <div>
    <h2>Continents</h2>
    </div>
    );
    }
    }

    export default Continents;


    And here is the App.js code:



    import React, { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';
    import Continents from './components/continents/continents';

    class App extends Component {
    render() {
    return (
    <div className="App">
    <header className="App-header">
    <img src={logo} className="App-logo" alt="logo" />
    <h1 className="App-title">Developed with NodeJS + React</h1>
    </header>
    <Continents />
    </div>
    );
    }
    }

    export default App;


    Issue:



    continents array is empty. No data fetched. However, there are no errors. I would very much appreciate if someone could guide me in the right direction to solve this. Thank you very much.










    share|improve this question



























      1












      1








      1







      I am trying to fetch the following results of my api (which is working well)
      http://localhost:5000/api/continents



      {"data":[{"continentId":3,"CName":"Atlantis"},{"continentId":2,"CName":"Devias"},{"continentId":1,"CName":"Lorencia"}]}


      into a react component (a simple array to begin with).



      Endpoint code extracted from server.js:



      app.get('/api/continents', (req, res) => {
      connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => {
      if(err){
      return res.send(err)
      }
      else{
      return res.json({
      data: results
      })
      }
      });
      });


      Here is my continents.js code:



      import React, { Component } from 'react';
      import './continents.css';

      class Continents extends Component {
      constructor() {
      super();
      this.state = {
      continents:
      }
      }

      ComponentDidMount() {
      fetch('http://localhost:5000/api/continents')
      .then(res => res.json())
      .then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
      }

      render() {
      return (
      <div>
      <h2>Continents</h2>
      </div>
      );
      }
      }

      export default Continents;


      And here is the App.js code:



      import React, { Component } from 'react';
      import logo from './logo.svg';
      import './App.css';
      import Continents from './components/continents/continents';

      class App extends Component {
      render() {
      return (
      <div className="App">
      <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h1 className="App-title">Developed with NodeJS + React</h1>
      </header>
      <Continents />
      </div>
      );
      }
      }

      export default App;


      Issue:



      continents array is empty. No data fetched. However, there are no errors. I would very much appreciate if someone could guide me in the right direction to solve this. Thank you very much.










      share|improve this question















      I am trying to fetch the following results of my api (which is working well)
      http://localhost:5000/api/continents



      {"data":[{"continentId":3,"CName":"Atlantis"},{"continentId":2,"CName":"Devias"},{"continentId":1,"CName":"Lorencia"}]}


      into a react component (a simple array to begin with).



      Endpoint code extracted from server.js:



      app.get('/api/continents', (req, res) => {
      connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => {
      if(err){
      return res.send(err)
      }
      else{
      return res.json({
      data: results
      })
      }
      });
      });


      Here is my continents.js code:



      import React, { Component } from 'react';
      import './continents.css';

      class Continents extends Component {
      constructor() {
      super();
      this.state = {
      continents:
      }
      }

      ComponentDidMount() {
      fetch('http://localhost:5000/api/continents')
      .then(res => res.json())
      .then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
      }

      render() {
      return (
      <div>
      <h2>Continents</h2>
      </div>
      );
      }
      }

      export default Continents;


      And here is the App.js code:



      import React, { Component } from 'react';
      import logo from './logo.svg';
      import './App.css';
      import Continents from './components/continents/continents';

      class App extends Component {
      render() {
      return (
      <div className="App">
      <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h1 className="App-title">Developed with NodeJS + React</h1>
      </header>
      <Continents />
      </div>
      );
      }
      }

      export default App;


      Issue:



      continents array is empty. No data fetched. However, there are no errors. I would very much appreciate if someone could guide me in the right direction to solve this. Thank you very much.







      javascript node.js reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 18:41









      mihai

      23.4k73968




      23.4k73968










      asked Nov 12 '18 at 22:38









      Razvan Stanciu

      294




      294
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Your set state is wrong,




          your nodejs is sending the object inside of data




          this.setState({continents: continents.data})




          your ComponentDidMount should be spelled with a lower c so it should be componentDidMount







          share|improve this answer

















          • 1




            Thank you very much, it is working now.
            – Razvan Stanciu
            Nov 13 '18 at 10:41



















          1














          @Razvan Can you please correct name of your componentDidMount method. It should be componentDidMount not ComponentDidMount. C must be lowercase.






          share|improve this answer





















          • It is working, Thank you! :)
            – Razvan Stanciu
            Nov 13 '18 at 10:43



















          1














          I think the data structure coming back from your API service call contains a data key. Try changing your setState to:



          this.setState({continents: continents.data})


          If that does not fix it, I'd double check the shape of the data structures you are passing around because the logic seems correct.






          share|improve this answer





















          • Thanks, there was also a problem with the spelling for componentDidMount() method.
            – Razvan Stanciu
            Nov 13 '18 at 10:42











          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%2f53271118%2freact-nodejs-fetch-issue%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your set state is wrong,




          your nodejs is sending the object inside of data




          this.setState({continents: continents.data})




          your ComponentDidMount should be spelled with a lower c so it should be componentDidMount







          share|improve this answer

















          • 1




            Thank you very much, it is working now.
            – Razvan Stanciu
            Nov 13 '18 at 10:41
















          1














          Your set state is wrong,




          your nodejs is sending the object inside of data




          this.setState({continents: continents.data})




          your ComponentDidMount should be spelled with a lower c so it should be componentDidMount







          share|improve this answer

















          • 1




            Thank you very much, it is working now.
            – Razvan Stanciu
            Nov 13 '18 at 10:41














          1












          1








          1






          Your set state is wrong,




          your nodejs is sending the object inside of data




          this.setState({continents: continents.data})




          your ComponentDidMount should be spelled with a lower c so it should be componentDidMount







          share|improve this answer












          Your set state is wrong,




          your nodejs is sending the object inside of data




          this.setState({continents: continents.data})




          your ComponentDidMount should be spelled with a lower c so it should be componentDidMount








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 22:58









          o elhajoui

          1198




          1198








          • 1




            Thank you very much, it is working now.
            – Razvan Stanciu
            Nov 13 '18 at 10:41














          • 1




            Thank you very much, it is working now.
            – Razvan Stanciu
            Nov 13 '18 at 10:41








          1




          1




          Thank you very much, it is working now.
          – Razvan Stanciu
          Nov 13 '18 at 10:41




          Thank you very much, it is working now.
          – Razvan Stanciu
          Nov 13 '18 at 10:41













          1














          @Razvan Can you please correct name of your componentDidMount method. It should be componentDidMount not ComponentDidMount. C must be lowercase.






          share|improve this answer





















          • It is working, Thank you! :)
            – Razvan Stanciu
            Nov 13 '18 at 10:43
















          1














          @Razvan Can you please correct name of your componentDidMount method. It should be componentDidMount not ComponentDidMount. C must be lowercase.






          share|improve this answer





















          • It is working, Thank you! :)
            – Razvan Stanciu
            Nov 13 '18 at 10:43














          1












          1








          1






          @Razvan Can you please correct name of your componentDidMount method. It should be componentDidMount not ComponentDidMount. C must be lowercase.






          share|improve this answer












          @Razvan Can you please correct name of your componentDidMount method. It should be componentDidMount not ComponentDidMount. C must be lowercase.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 22:47









          Rutul Patel

          157111




          157111












          • It is working, Thank you! :)
            – Razvan Stanciu
            Nov 13 '18 at 10:43


















          • It is working, Thank you! :)
            – Razvan Stanciu
            Nov 13 '18 at 10:43
















          It is working, Thank you! :)
          – Razvan Stanciu
          Nov 13 '18 at 10:43




          It is working, Thank you! :)
          – Razvan Stanciu
          Nov 13 '18 at 10:43











          1














          I think the data structure coming back from your API service call contains a data key. Try changing your setState to:



          this.setState({continents: continents.data})


          If that does not fix it, I'd double check the shape of the data structures you are passing around because the logic seems correct.






          share|improve this answer





















          • Thanks, there was also a problem with the spelling for componentDidMount() method.
            – Razvan Stanciu
            Nov 13 '18 at 10:42
















          1














          I think the data structure coming back from your API service call contains a data key. Try changing your setState to:



          this.setState({continents: continents.data})


          If that does not fix it, I'd double check the shape of the data structures you are passing around because the logic seems correct.






          share|improve this answer





















          • Thanks, there was also a problem with the spelling for componentDidMount() method.
            – Razvan Stanciu
            Nov 13 '18 at 10:42














          1












          1








          1






          I think the data structure coming back from your API service call contains a data key. Try changing your setState to:



          this.setState({continents: continents.data})


          If that does not fix it, I'd double check the shape of the data structures you are passing around because the logic seems correct.






          share|improve this answer












          I think the data structure coming back from your API service call contains a data key. Try changing your setState to:



          this.setState({continents: continents.data})


          If that does not fix it, I'd double check the shape of the data structures you are passing around because the logic seems correct.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 22:49









          Samuel

          1,5861529




          1,5861529












          • Thanks, there was also a problem with the spelling for componentDidMount() method.
            – Razvan Stanciu
            Nov 13 '18 at 10:42


















          • Thanks, there was also a problem with the spelling for componentDidMount() method.
            – Razvan Stanciu
            Nov 13 '18 at 10:42
















          Thanks, there was also a problem with the spelling for componentDidMount() method.
          – Razvan Stanciu
          Nov 13 '18 at 10:42




          Thanks, there was also a problem with the spelling for componentDidMount() method.
          – Razvan Stanciu
          Nov 13 '18 at 10:42


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53271118%2freact-nodejs-fetch-issue%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

          Bressuire

          Vorschmack

          Quarantine