0

I have a MongoDB that I can pull data from inside of Postman like this: http://localhost:8080/employees. This works just fine, but if I make the same request from my react front end, then nothing happens.

This is my index.js file:

ReactDOM.render(
      <App />,
  document.getElementById('root')
);

This is my App.js file:

import './App.css';

import ListEmployeeComponent from './components/ListEmployeeComponent';

function App() {
  return (
    <div className="container">
      <ListEmployeeComponent/>
    </div>
  );
}

export default App;

And this is the component that I need to show the data from the MongoDB on:

class ListEmployeeComponent extends Component {
    constructor(props){
        super(props)
        
        this.state = {
            employees: []
        }
    }

    compondentDidMount(){
        EmployeeService.getEmployees().then((res)=> {
            this.setState({employees: res.data})
        });
    }

    render() {
        return (
            <Fragment>
                {console.log(this.props)}
                <h2 className="text-center">Employee List</h2>
                <div className="row">
                    <table className="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Employee First Name</th>
                                <th>Employee Last Name</th>
                                <th>Employee Email Id</th>
                                <th>Actions</th>

                            </tr>
                        </thead>
                        <tbody>
                            {
                                this.state.employees.map(
                                    employee =>
                                    <tr key = {employee.id}>
                                        <td>{employee.firstName}</td>
                                        <td>{employee.lastName}</td>
                                        <td>{employee.emailId}</td>
                                    </tr>
                                )
                            }
                        </tbody>
                    </table>
                </div>
            </Fragment>
        );
    }
}


export default ListEmployeeComponent;

The Javascript console only has this one message as far as I can tell: [HMR] Waiting for update signal from WDS... So what's the deal? Why am I not getting a response from React but I do in Postman and in my URL.

And finally, this is where I am using axios:

import axios from 'axios';

const EMPLOYEES_API_BASE_URL = "http://localhost:8080/employees";

class EmployeeService {
    getEmployees(){
        console.log("hello");
        return axios.get(EMPLOYEES_API_BASE_URL);
    }
}

// Not exporting the class, but an object of this class so that we can use the object to call these methods ^
export default new EmployeeService();

As you can see, there is no employee data on my page: Image

EDIT: This is my Network tab: Network tab

This is the controller in the backend:

@CrossOrigin(origins = "http://localhost:3000")
@RestController
public class EmployeeController {

    // Auto-inject EmployeeRepository Class into this class so we can use the object
    // without calling 'new EmployeeRepository'
    @Autowired
    private EmployeeRepository employeeRepo;

    // GET all employees
    @GetMapping("/employees")
    public List<Employee> getAllEmployees() {
        return employeeRepo.findAll();
    }

}
9
  • Do you see the request in the network tab? Is CORS set up on the server correctly? We will be needing more information in order to help you with this problem. Commented Sep 1, 2021 at 14:45
  • Okay, I added a screen shot from the network tab. Does that look correct? Commented Sep 1, 2021 at 14:55
  • I also have @CrossOrigin in my controller. I will add that to my question. Commented Sep 1, 2021 at 15:00
  • I don't see the request in the network panel. Are you sure that the getEmployees method is being called? Commented Sep 1, 2021 at 15:06
  • 1
    It should be componentDidMount not compondentDidMount. Commented Sep 1, 2021 at 15:17

1 Answer 1

1

As you can see the api is not being called, so the problem rests in your api call flow. You could track where the problem is by putting debug prints (it is better to use more meaningful logs than hello) in every step of your app's flow.

If you do this you would notice something is wrong with the componentDidMount() function as it is not being called. It is a simple typo.

Sign up to request clarification or add additional context in comments.

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.