I'm calling my api inside componentDidMount and I want to set state to use the values later on, but I'm getting nulls. The API itself works, tested with postman, also calling other methods also works inside the componentDidMount.
componentDidMount() {
let url = window.location.pathname;
let id = url.substring(url.lastIndexOf('/') + 1);
console.log(id)
userService.getDepartment(id).then(departments => this.setState({ ...departments }));
console.log(this.state.address);
}
This is what I see in the console log.
https://i.stack.imgur.com/aNF4l.png
My get function:
function getDepartment(id) {
const requestOptions = { method: 'GET', headers: authHeader() };
return fetch(`${config.apiUrl}/api/department/${id}`, requestOptions);
}