简体   繁体   English

链接 axios.get 调用以使用 axios.all 多个请求的响应

[英]Chaining an axios.get call to use the response for an axios.all multiple request

I'm having difficulties to understand how Shall I handle this type of calls.我很难理解我应该如何处理这类电话。 I need an initial axios.get call and loop through to make an axios.all call.我需要一个初始 axios.get 调用并循环进行 axios.all 调用。 This is my code:这是我的代码:

export async function getServerSideProps(context) {
  const axios = require("axios");

  const options = {
    method: 'GET',
    url: 'xxxxx',
    params: { id_user: 'xxxxx' },
    headers: {
      'X-RapidAPI-Host': 'xxxxxxx',
      'X-RapidAPI-Key': 'xxxxxxx'
    }
  };



  const res = axios.request(options).then(function (response) {

    axios.all(response.data.users.map(user => {
      axios.get('xxxxxxx', {
        params: { response_type: 'short', ig: user.username, corsEnabled: 'true' },
        headers: {
          'X-RapidAPI-Host': 'xxxxx',
          'X-RapidAPI-Key': 'xxxxxxx'
        }
      })
    })).then(res => {
      return res.data
    })

  })

  const payload = await res;

I have an error on the page when I try to console log the payload.当我尝试控制台记录负载时,页面上出现错误。 What's going worng with my call?我的电话怎么了?

Looks like you need a return inside your map.看起来你需要在你的 map 里面return

The below code worked for me and the unique params were updated on each request.下面的代码对我有用,并且每个请求都会更新唯一的参数。 Once you prepare your series of endpoints or whatever is unique for each request (user data per your sample code) you map over the relevant data and make all your get requests.一旦您准备好一系列端点或每个请求唯一的任何内容(每个示例代码的用户数据),您 map 就可以获取相关数据并发出所有get请求。 The result returned will be an array of the responses you get back from each request once all of the requests have finished.所有请求完成后,返回的结果将是您从每个请求返回的响应数组。

Regarding chaining together requests, you can use any number of .then() to kick off other actions.关于将请求链接在一起,您可以使用任意数量的.then()来启动其他操作。 You mention needing an id from an initial get request, so here is a solution based on that example.您提到需要来自初始 get 请求的 id,因此这是基于该示例的解决方案。

Hope you find it helpful.希望你觉得它有帮助。

const users = [{ username: '1' }, { username: '2' }, { username: '3' }];
axios
    .get('https://jsonplaceholder.typicode.com/posts')
    .then((res) => {
      console.log(res);
      // simulating getting some id you need...
      const id = res.data[4].id;
      console.log(id);

      return id;
    })
    .then((id) => {
      axios
        .all(
          users.map((user) => {
            return axios.get('https://jsonplaceholder.typicode.com/posts', {
              params: {
                response_type: 'short',
                // just an example of using id...
                importantId: id,
                ig: user.username,
                corsEnabled: 'true',
              },
              headers: {
                'X-RapidAPI-Host': 'xxxxxxx',
                'X-RapidAPI-Key': 'xxxxxxx',
              },
            });
          })
        )
        .then((res) => {
          console.log(res);
        });
    });

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

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