0

So I want to call an API with react and my data is not showing up correctly I know I did it right but is there anything I am missing because I don't know why it is still showing Loading

Web screenshot

   import React from 'react';
import './App.css';

class App extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
      students: [],
      isLoaded: false,
      error: null,
    }
  }

  componentDidMount() {
    fetch("http://localhost:3200/students")
    .then(res => res.json())
    .then((myresult) => {
      this.setState({
           isLoaded: true,
           students: myresult.students,
      });

  },
  (error) => {
    this.setState({
         isLoaded: true,
         error
    });
  }
)
}
render() {
    const {error, isLoaded, students} = this.state;
    if (error) {
      return <div>Error: {error.message}</div>
    } else if (isLoaded) {
      return <div> Loading ...</div>
    } else {
  return (
    <ul>
      {students && students.map(student => (
       <li key={student._id}>
          {student._id} {student.role_num}

       </li>
      ))}
  </ul>

  );
}
}
}

export default App;

I am trying to display information from this URL http://localhost:3200/students

I was following this link on how to call an API https://reactjs.org/docs/faq-ajax.html hey guys updated my code I am still getting the same issue stuck on loading I don't know whether I am passing in my data properly

1
  • I guess that by MountComponent you meant componentDidMount right ? Although, you are never setting the students property in your state Commented Jul 1, 2019 at 12:01

4 Answers 4

1

I think there a couple of things going on here but main points are

  • its componentDidMount not MountComponent
  • your not setting students correctly in state
Sign up to request clarification or add additional context in comments.

Comments

0

In you code above for MountComponent see that you are setting up movieItems and your state is expecting students. To solve the issue use the code below.

MountComponent = () => {
    fetch("http://localhost:3200/students")
    .then(res => res.json())
    .then((myresult) => {
      this.setState({
           isLoaded: true,
           students: myresult.students,
      });

  },
  (error) => {
    this.setState({
         isLoaded: true,
         error
    });
  }
)
  }

5 Comments

he never calls MountComponent on his code, so you are right about students but still, this is a partial answer. either tell him to change it to CDM or tell him where and how to invoke it
@Roy.B so I have updated my code and posted it on SO but still get loading
@Arnald that's because the if(!isLoaded), change it to if(isLoaded)
@Roy.B ok fixed it now I get this error Cannot read property 'map' of undefined
In your render function above you do not validate whether students is defined or not. change the code in render method as below What i added is students && before students.map do this and try in your code <ul> {students && students.map(student => ( <li key={student._id}> {student._id} {student.role_num} </li> ))} </ul>
0

change this:

} else if (!isLoaded) {

to:

} else if (isLoaded) {

3 Comments

@Arnald see my answer
can you console log myresult ?
.then((myresult) => {
0

Write this in your function and check

componentDidMount() {
  fetch("http://localhost:3200/students").then(res => {
  this.setState({ students : res.data })
  })
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.