[英]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:我对express有点陌生,我目前正在使用thedogapi,我正在做后端,我需要对api(气质)的确切部分进行GET,这给我带来了很多问题,一个例子api 是:
},
"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抱歉,如果有点长,我对这一切都很陌生,我真的被困在这里:s
According to the API reference:根据 API 参考:
https://docs.thedogapi.com/api-reference/breeds/breeds-list 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.我给你一个例子供你参考,我参考你的代码,变量temperamentsApi存储API结果,所以提取代码应该如下。
let result=[]; temperamentsApi.data.forEach(datum=>{ result.push(datum.temperament); }); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.