I have multiple get requests with axios.all
in my Node.js backend. I want to return the response to the function and then consume this as API in ReactJS. When I use console.log
to output the data in the React component, I get Promise { <state>: "pending" }
, but I want to get data from the Node.js backend.
Could you please help me?
Node.js backend:
export async function getArrivalPorts() {
let endpoints = [
'https://api.github.com/users/ejirocodes',
'https://api.github.com/users/ejirocodes/repos',
'https://api.github.com/users/ejirocodes/followers',
'https://api.github.com/users/ejirocodes/following'];
const response = await axios.all(endpoints.map((endpoint) => axios.get(endpoint)))
.then(axios.spread((
{data: user},
{data: repos},
{data: followers},
{data: following}) => {
return ({user, repos, followers, following});
}))
.catch(errors => {
console.log(errors)
})
return(response)
}
Node.js router
router.get('/arrival', getArrivalPorts)
ReactJS frontend
function From ()
{
const data = axios.get('http://localhost:5000/arrival')
console.log(data)
}