I have a one reactjs project using axios to post to backend API.
The problem is that I need to wrap 2 axios post in one async function, 1 post is getting bookId and then call another function using the getting bookId to get book details. These 2 functions are in one async function getBookDetails wrapping in useEffect. Both axios Posts will set Authorization token which using Auth0.
After running the project, the first call run successfully, but the 2nd call always get 401 error from backend.
const BookStore = () => {
useEffect(() => {
const getBook = async() => {
const {data: bookId} = await getBookId(name:string)
const {data: bookDetails} = await getBookDetails(Id:string)
}
getBook()
}
)
}
export function getBookId(name:string) {
return axios.post<string>('url', {name}, {
headers: {
"content-type": "application/json",
Authorization: `Bearer ${token}`
}
}
export function getBookDetail(id:string) {
return axios.post<string>('url', {id}, {
headers: {
"content-type": "application/json",
Authorization: `Bearer ${token}`
}
}
2
Answers
And make sure to pass the token to the API functions
Instead of use promise all you should go with this