简体   繁体   中英

How would you extract this info from this api?

I'm kind of new with express, I'm currently working with thedogapi, I am doing the backend and I need to make a GET to an exact part of the api (the temperaments) that's bringing me a lot of problems, an example of the api is:

    },
"id": 1,
"name": "Affenpinscher",
"bred_for": "Small rodent hunting, lapdog",
"breed_group": "Toy",
"life_span": "10 - 12 years",
"temperament": "Stubborn, Curious, Playful, Adventurous, Active, Fun-loving",
"origin": "Germany, France",
"reference_image_id": "BJa4kxc4X",
"image": {
"id": "BJa4kxc4X",
"width": 1600,
"height": 1199,
"url": "https://cdn2.thedogapi.com/images/BJa4kxc4X.jpg"
}

As you can see, my problem it's that it is only a string with words inside, the thing here would be extracting only the temperaments so I can save them on my database once extracted, but the question is, how could I do that? I have been trying a few things, but doesn't seems to work... A code I was trying was

const getTemp = await Temperament.findAll()
if (getTemp.length === 0){
  const apiAxios = await axios.get(`https://api.thedogapi.com/v1/breeds?api_key=${API_KEY}`)
  
  const infoToGet = await apiAxios.data?.map(el => {
    console.log(el.temperament?.split(",").map(el => el.trim()).toString())
    return {
      temperament: [el.temperament]?.join().split(",").map(el => el.trim()).toString()
    }
  })
  const dbSave = await 

Temperament.bulkCreate(infoToGet)
}

I totally discarded that code since it wasn't achieven what I wanted, but the part of the temperaments was kinda working according to the console log, but still wasn't working, so then I received kind of a reference for help, but this wouldn't make it since it's using array methods... I'll show you (that code bellow should work if it was an array...)

router.get("/temperaments", async (req, res) => {
    const temperamentsApi = await axios.get(`https://api.thedogapi.com/v1/breeds?api_key=${API_KEY}`)
    const temperaments = temperamentsApi.data.map( el => el.temperament)
    const tempEach = temperaments.map(el => {
        for (let i = 0; i < el.length; i++) return el[i]})
        console.log(tempEach)
    tempEach.forEach(el => {
        Temperament.findOrCreate({
            where: { temperament: el }
        })
    })

    const allTemperaments = await Temperament.findAll()
    res.send(allTemperaments)
})

how would you make this last thing work on the TEMPERAMENT part alone? sorry if it was kinda long, I'm new in all this, and I'm truly stuck here:s

According to the API reference:

https://docs.thedogapi.com/api-reference/breeds/breeds-list

I give you an example for your reference, I refer to your code, the variable temperamentsApi stores the API result, so the extraction code should be as below.

 let result=[]; temperamentsApi.data.forEach(datum=>{ result.push(datum.temperament); }); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM