WDI-SEA / project-2-issues

0 stars 0 forks source link

Pulling info from multiple api routes #2

Closed paulinal3 closed 2 years ago

paulinal3 commented 2 years ago

I'm trying to pull out all the info from my api data for a detailed exercise page that will list the name, major muscle group, primary muscle worked. etc. The problem is that the info returned in main results have those categories listed as ids because then I need to enter a different parameter at the end of my root api in order to figure out the name to the ids given. For example this is what comes back from one exercise.

https://wger.de/api/v2/exercise

id: 343,
uuid: '1b9dc5bc-790b-4e21-a55d-f8b3115e94c5',
name: 'Barbell Ab Rollout',
category: 10,
muscles: [ 14 ],

So my details page on the frontend looks like image

So I would then have to go to https://wger.de/api/v2/exercisecategory to pull out that category 10 = abs to have that display as the muscle group on my page

"results": [
        {
            "id": 10,
            "name": "Abs"
        },
        {
            "id": 8,
            "name": "Arms"
        },
        {
            "id": 12,
            "name": "Back"
        },
        {
            "id": 14,
            "name": "Calves"
        },
        {
            "id": 11,
            "name": "Chest"
        },
        {
            "id": 9,
            "name": "Legs"
        },
        {
            "id": 13,
            "name": "Shoulders"
        }
    ]

I'd have to do the same for the primary and secondary muscles worked with a different url.

My route looks like this:

router.get('/:exercise_id', (req, res) => {
    let exerciseId = req.params.exercise_id
    const exerciseApi = 'https://wger.de/api/v2/exercise/'
    const eng = 'language=2'

    axios.get(`${exerciseApi}${exerciseId}?${eng}`)
    .then(apiRes => {
        console.log('this is apiRes.data \n', apiRes.data)
        let name = apiRes.data.name
        let muscleGroup = apiRes.data.category
        let description = apiRes.data.description
        let primaryMuscle = apiRes.data.muscles[0]
        let secondaryMuscle = apiRes.data.muscles[0]

        res.render('search/details', {name, muscleGroup, description, primaryMuscle, secondaryMuscle})
    })
    .catch(error => {
        console.error
    })
})

Can I/would I have to input multiple axios.get with the correct url and create variables/gather info and compare all the data I'm getting back that way or would I have to create my own database that stores all names and id and compare the api results to my own db?

DoireannJane commented 2 years ago

.all() maybe?

paulinal3 commented 2 years ago

created a nested axios call