I'm putting my API calls in a new function so I can easily call them with a couple of lines instead of a ton of code on every page. The problem I'm having is that the async and await is completing before returning the data.
I'm getting console.log(sess.getIdToken().getJwtToken()) consoled first, followed by undefined for console.log(getMessages), then the data from console.log(response).
So the await Auth.currentSession().then(...) runs but does't wait for the axios call inside of it. How do I return the data so I can access it in the useEffect?
useEffect(() => {
async function getMessages() {
const getMessages = await ApiService.getMessages();
console.log(getMessages)
}
getMessages();
}, []);
async getMessages() {
return await this.__sendRequest("/default/message", "GET");
}
async __sendRequest(url, method) {
await Auth.currentSession().then(sess => {
console.log(sess.getIdToken().getJwtToken())
axios({
method: method,
url: process.env.REACT_APP_USER_URL + url,
headers: {
"X-TOKEN-ID": sess.getIdToken().getJwtToken(),
"addresseeType": "P",
"content-type": "application/json"
}
}).then(function (response) {
console.log(response)
return response
}).catch(err => {
console.error(err);
})
})
}