WDI-SEA / project-2-issues

0 stars 0 forks source link

Nested axios call #32

Closed harrdev closed 2 years ago

harrdev commented 2 years ago

I'm looking for help trying to figure out how to do a nested axios call. There is a URL for an API request that is returned in the original axios.get. I need to access that url in another axios call after the original call to display a property within that second axios call.

I tried putting another axios call in a .then within the route but it didn't work.

TaylorDarneille commented 2 years ago

that is how you do it, so post your code here in a snippet so we can see it :)

harrdev commented 2 years ago

router.get('/:person', function(req, res) {
    let person = req.params.person
    axios.get(`https://swapi.dev/api/people/?search=${person}`)
    .then(apiRes => {
        let name = apiRes.data.results[0].name
        let homeworld = apiRes.data.results[0].homeworld
        console.log("homeworld: ", homeworld)
        res.render('academyPeople.ejs', { name })
    })
    .then(apiRes => {
        axios.get(homeworld)
        .then(apiRes => {
            console.log(homeworld)
        })
    })
})```
harrdev commented 2 years ago

I don't know how to pass that variable homeworld into the second axios call. It is created from the first call, it is a URL to another API resource that I need to access to extract data out of itl.