简体   繁体   English

如何执行两个 Axios GET 请求, map 结果然后将其推送到数组?

[英]How can I do two Axios GET requests, map the result and then pushing that to an array?

I have a hard time understanding promises and in this case with Axios.我很难理解承诺,在这种情况下是 Axios。 I've been reading up about it and searched stackoverflow endlessly but still can't wrap my head around it.我一直在阅读它并无休止地搜索stackoverflow,但仍然无法理解它。

Firstly, I'm trying to get a list of exercises, and in that result there is an ID(called exercise_base).首先,我试图获取练习列表,结果是一个 ID(称为练习库)。 That ID i want to use to make another GET request to receive the images for that exercise.我想使用该 ID 发出另一个 GET 请求以接收该练习的图像。

Then, I'm pushing the name, id, and the image as an object to an array.然后,我将名称、id 和图像作为 object 推送到数组。 It works perfectly to get the list of exercises and push that to an array, but when trying to get the images I can't seem to get it working.它可以完美地获取练习列表并将其推送到数组中,但是在尝试获取图像时,我似乎无法使其正常工作。

In my object, I want to pass the imageUrl that I receive from my getImages promise.在我的 object 中,我想传递从我的 getImages promise 收到的 imageUrl。 How can I achieve this?我怎样才能做到这一点?

function getImages(exercise_base) {
  return axios.get("https://wger.de/api/v2/exerciseimage/?exercise_base=" + exercise_base);
}

const fetchData = async () => {
  const result = await axios(getFetchUrl());
  const array = [];
  // mapping through all the exercises, getting the exercise_base id which i then pass my getImages function
  result.data.results.map(({
    name,
    id,
    category,
    description,
    exercise_base
  }, e, index) => {
    getImages(exercise_base).then((e) => {
      // I want to pass this as imageUrl: in my object
      console.log(e.data.results[0].image);
    });
    array.push({
      value: name,
      description: "description",
      category: category,
      key: id,
      imageUrl: "" // Here I want to pass my imageUrl that I get from my getImages promise.
    });
  });
};

Create a single promise that resolves when all the inner promises resolve using Promise.all() .创建一个单一的 promise ,当所有内部承诺使用Promise.all()解析时解析。 The resolution of each inner promise can be the fully hydrated object每个内promise的分辨率可以是全水化object

const EXERCISE_IMAGE_URL = "https://wger.de/api/v2/exerciseimage/"

const getMainImage = async (exercise_base) => {
  const { data: { results } } = await axios.get(EXERCISE_IMAGE_URL, {
    params: { exercise_base } // using params is safer
  })

  // resolve with the first _main_ image (or undefined if none)
  return results.find(({ is_main }) => is_main)?.image
}

const fetchData = async () => {
  const { data: { results } } = await axios(getFetchUrl());

  // map each result to a new promise that resolves with the full object
  return Promise.all(results.map(async ({
    name,
    id,
    category,
    description,
    exercise_base
  }) => ({
    value: name,
    description,
    category,
    key: id,
    imageUrl: await getMainImage(exercise_base)
  })))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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