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:

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();
}
}

getEmployeesmethod is being called?componentDidMountnotcompondentDidMount.